
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if both halves of the string have at least one different character in Python
Suppose we have a lowercase string; we have to check whether we can split the string from middle which will give two halves having at least one-character difference between two sides. It may hold different characters or different frequency of each character. If the string is odd length string, then ignore the middle element and check for the remaining elements.
So, if the input is like s = "helloohekk", then the output will be True as "helloohekk" so left part is "hello" right part is "ohekk" left and right are different.
To solve this, we will follow these steps −
- left_freq := an empty map
- right_freq := an empty map
- n := size of s
- for i in range 0 to quotient of (n/2) - 1, do
- left_freq[s[i]] := left_freq[s[i]] + 1
- for i in range quotient of (n/2) to n - 1, do
- right_freq[s[i]] := right_freq[s[i]] + 1
- for each char in s, do
- if right_freq[char] is not same as left_freq[char], then
- return True
- if right_freq[char] is not same as left_freq[char], then
- return False
Let us see the following implementation to get better understanding −
Example
from collections import defaultdict def solve(s): left_freq = defaultdict(int) right_freq = defaultdict(int) n = len(s) for i in range(n//2): left_freq[s[i]] += 1 for i in range(n//2, n): right_freq[s[i]] += 1 for char in s: if right_freq[char] != left_freq[char]: return True return False s = "helloohekk" print(solve(s))
Input
"helloohekk"
Output
True
- Related Articles
- Check if both halves of the string have same set of characters in Python
- Check if both halves of the string have the same set of characters in Python
- Python program to check if both halves of the string have same set of characters.
- Check if both halves of the string have same set of characters in C#
- Python program to check if two lists have at least one common element
- How to check if a string has at least one letter and one number in Python?
- C# program to check if two lists have at-least one element common
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string in Python
- Check if max occurring character of one string appears same no. of times in other in Python
- Java regex program to verify whether a String contains at least one alphanumeric character.
- Program to check whether String Halves Are Alike in Python
- How to check if a string contains only one type of character in R?
- Python program to find Least Frequent Character in a String
- Python - Check if frequencies of all characters of a string are different
- Program to check if a string contains any special character in Python

Advertisements