Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Check if frequencies of all characters of a string are different
In this article, we will see how to find the frequency of each character in a given string and check if all characters have different frequencies. We'll accomplish this in two steps: first calculating character frequencies, then checking if all frequencies are unique.
Finding Character Frequencies
We can count character frequencies using a dictionary. For each character in the string, we either increment its count or initialize it to 1 ?
Example
in_string = "She sells sea shells"
char_freq = {}
for char in in_string:
if char in char_freq.keys():
char_freq[char] += 1
else:
char_freq[char] = 1
print("Character frequencies:", char_freq)
for char in char_freq.keys():
print(f"{char!r} repeats {char_freq[char]} time(s)")
Character frequencies: {'S': 1, 'h': 2, 'e': 4, ' ': 3, 's': 5, 'l': 4, 'a': 1}
'S' repeats 1 time(s)
'h' repeats 2 time(s)
'e' repeats 4 time(s)
' ' repeats 3 time(s)
's' repeats 5 time(s)
'l' repeats 4 time(s)
'a' repeats 1 time(s)
Checking if All Frequencies Are Different
To check if all characters have different frequencies, we extract all frequency values and convert them to a set. If the set length equals the dictionary length, all frequencies are unique ?
Example
in_string = "She sells sea shells"
char_freq = {}
for char in in_string:
if char in char_freq.keys():
char_freq[char] += 1
else:
char_freq[char] = 1
print("Character frequencies:", char_freq)
# Get unique frequency values
unique_frequencies = set(char_freq.values())
print("Unique frequencies:", unique_frequencies)
print("Number of unique frequencies:", len(unique_frequencies))
if len(unique_frequencies) == len(char_freq):
print("All characters have different frequencies")
else:
print("Some characters have the same frequency")
Character frequencies: {'S': 1, 'h': 2, 'e': 4, ' ': 3, 's': 5, 'l': 4, 'a': 1}
Unique frequencies: {1, 2, 3, 4, 5}
Number of unique frequencies: 5
Some characters have the same frequency
Using Collections.Counter Method
Python's collections.Counter provides a more elegant solution for counting character frequencies ?
from collections import Counter
def check_different_frequencies(string):
# Count character frequencies
char_freq = Counter(string)
print("Character frequencies:", dict(char_freq))
# Get unique frequency values
frequencies = list(char_freq.values())
unique_frequencies = set(frequencies)
print("All frequencies:", frequencies)
print("Unique frequencies:", unique_frequencies)
# Check if all frequencies are different
if len(unique_frequencies) == len(frequencies):
return "All characters have different frequencies"
else:
return "Some characters have the same frequency"
# Test with different strings
test1 = "abc"
test2 = "She sells sea shells"
print("Testing:", test1)
print(check_different_frequencies(test1))
print("\nTesting:", test2)
print(check_different_frequencies(test2))
Testing: abc
Character frequencies: {'a': 1, 'b': 1, 'c': 1}
All frequencies: [1, 1, 1]
Unique frequencies: {1}
Some characters have the same frequency
Testing: She sells sea shells
Character frequencies: {'S': 1, 'h': 2, 'e': 4, ' ': 3, 's': 5, 'l': 4, 'a': 1}
All frequencies: [1, 2, 4, 3, 5, 4, 1]
Unique frequencies: {1, 2, 3, 4, 5}
Some characters have the same frequency
Conclusion
To check if all characters have different frequencies, count character occurrences using a dictionary or Counter, then compare the number of unique frequencies with the total number of characters. Use set(frequencies) to find unique values efficiently.
