Python | Check if frequencies of all characters of a string are different
Last Updated :
23 Mar, 2023
Given a string S consisting only of lowercase letters, the task is to check if the frequency of all characters of the string is unique.
Examples:
Input : abaccc
Output : Yes
‘a’ occurs two times, ‘b’ occurs once
and ‘c’ occurs three times.
Input : aabbc
Output : No
Frequency of both 'a' and 'b' are same.
Approach: One thing to observe in this problem is that position of characters does not matter here so, simply count the frequency of characters. Go through the string and count the occurrence of all characters. Then sort the frequency array and check if consecutive elements are same and immediately print "No" else print "Yes".
Python3
# Python program to check if frequency of
# all characters of a string are unique
# creating a frequency array
freq =[0]*26
# Finding length of s
n = len(s)
for i in range(n):
# counting frequency of all characters
freq[ord(s[i])-97] += 1
# sorting the frequency array
freq.sort()
for i in range(25):
# checking if element is zero
if (freq[i] == 0):
continue
# if element is non-zero
# checking if frequencies are unique
if (freq[i] == freq[i + 1]):
return False
return True
# Driver code
s ="abaccc"
if(check(s)):
print("Yes")
else:
print("No")
Approach : Using count() and len() methods.First find the count of each element, append the count to a list. If the count of first element of list equals to the length of list, then the frequencies are unique.
Python3
# Python program to check if frequency of
# all characters of a string are unique
s ="aabbc"
v=[]
for i in s:
if i not in v:
v.append(s.count(i))
x=[]
for i in v:
if i not in x:
x.append(i)
if(len(x)==len(v)):
print("Yes")
else:
print("No")
Approach: using operator.countOf() method
Python3
# Python program to check if frequency of
# all characters of a string are unique
import operator as op
s ="aabbc"
v=[]
for i in s:
if i not in v:
v.append(op.countOf(s,i))
x=[]
for i in v:
if i not in x:
x.append(i)
if(len(x)==len(v)):
print("Yes")
else:
print("No")
Time Complexity: O(N)
Auxiliary Space : O(N)
Using counter and set()
Algorithm:
- Import the Counter module from the collections package
- Convert the input string into a Counter object
- Extract the frequency of each character and store in a set
- If the length of the set is equal to the number of unique frequencies, return "Yes"
- Otherwise, return "No"
Python3
from collections import Counter
def check_freq(s):
# Convert string into a Counter object
freq = Counter(s)
# Extract the frequency of each character and store in a set
freq_set = set(freq.values())
# Check if the length of the set is equal to the number of unique frequencies
if len(freq_set) == len(set(s)):
return "Yes"
else:
return "No"
# Driver code
s = "abaccc"
print(check_freq(s))
Complexity analysis:
Time complexity: The Counter function has a time complexity of O(N) where N is the length of the string. The set function also has a time complexity of O(N). Therefore, the overall time complexity of the code is O(N).
Space complexity: The space complexity of the code is O(N) because we are creating a Counter object and a set object, both of which can store up to N elements.
Similar Reads
Python program to check if a string contains all unique characters To implement an algorithm to determine if a string contains all unique characters. Examples: Input : s = "abcd" Output: True "abcd" doesn't contain any duplicates. Hence the output is True. Input : s = "abbd" Output: False "abbd" contains duplicates. Hence the output is False. One solution is to cre
3 min read
Program to check if all characters have even frequency Given a string S consisting only of lowercase letters check if the string has all characters appearing even times. Examples: Input : abaccaba Output : Yes Explanation: 'a' occurs four times, 'b' occurs twice, 'c' occurs twice and the other letters occur zero times. Input: hthth Output : No Approach:
7 min read
Count frequencies of all elements in array in Python using collections module Given an unsorted array of n integers which can contains n integers. Count frequency of all elements that are present in array. Examples: Input : arr[] = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 5] Output : 1 -> 4 2 -> 4 3 -> 2 4 -> 1 5 -> 2 This problem can be solved in many ways, refer
2 min read
Count occurrences of a character in string in Python We are given a string, and our task is to count how many times a specific character appears in it using Python. This can be done using methods like .count(), loops, or collections.Counter. For example, in the string "banana", using "banana".count('a') will return 3 since the letter 'a' appears three
2 min read
Find all duplicate characters in string in Python In this article, we will explore various methods to find all duplicate characters in string. The simplest approach is by using a loop with dictionary.Using Loop with DictionaryWe can use a for loop to find duplicate characters efficiently. First we count the occurrences of each character by iteratin
2 min read