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
Check whether the frequencies of all the characters in a string are prime or not in Python
Sometimes we need to check whether the frequencies of all characters in a string are prime numbers. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
So, if the input is like s = "apuuppa", then the output will be True as there are two 'a's, three 'p's and two 'u's. Since 2 and 3 are prime numbers, all character frequencies are prime.
Algorithm
To solve this problem, we will follow these steps ?
- Create a frequency map containing all characters and their counts
- For each character frequency, check if it's a prime number
- If any frequency is not prime, return False
- If all frequencies are prime, return True
Example
Let us see the implementation to get better understanding ?
from collections import defaultdict
def isPrime(num):
if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
return False
def solve(s):
freq = defaultdict(int)
# Count frequency of each character
for char in s:
freq[char] += 1
# Check if all frequencies are prime
for char in freq:
if not isPrime(freq[char]):
return False
return True
s = "apuuppa"
print(f"String: {s}")
print(f"Result: {solve(s)}")
# Let's see the frequencies
freq = defaultdict(int)
for char in s:
freq[char] += 1
print("\nCharacter frequencies:")
for char, count in freq.items():
print(f"'{char}': {count} (Prime: {isPrime(count)})")
String: apuuppa Result: True Character frequencies: 'a': 2 (Prime: True) 'p': 3 (Prime: True) 'u': 2 (Prime: True)
Testing with Another Example
Let's test with a string where not all frequencies are prime ?
def isPrime(num):
if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
return False
def solve(s):
freq = {}
# Count frequency of each character
for char in s:
freq[char] = freq.get(char, 0) + 1
# Check if all frequencies are prime
for char in freq:
if not isPrime(freq[char]):
return False
return True
test_string = "aaaa"
print(f"String: {test_string}")
print(f"Result: {solve(test_string)}")
# Show frequencies
freq = {}
for char in test_string:
freq[char] = freq.get(char, 0) + 1
print("\nCharacter frequencies:")
for char, count in freq.items():
print(f"'{char}': {count} (Prime: {isPrime(count)})")
String: aaaa Result: False Character frequencies: 'a': 4 (Prime: False)
Key Points
- We optimize the prime checking function by only checking divisors up to ?num
- Character frequency counting can be done using
defaultdictor regular dictionary - Numbers 1 and below are not considered prime
- The function returns False as soon as any non-prime frequency is found
Conclusion
This solution efficiently checks if all character frequencies in a string are prime numbers. The time complexity is O(n + k?m) where n is string length, k is unique characters, and m is the maximum frequency.
