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 if frequency of character in one string is a factor or multiple of frequency of same character in other string in Python
When working with two strings, we sometimes need to check whether the frequency of each character in one string is a factor or multiple of the frequency of the same character in another string. This means for each common character, one frequency should be divisible by the other.
So, if the input is like s = "xxyzzw" and t = "yyyxxxxzz", then the output will be True because:
- Frequency of 'x' in s is 2, and in t is 4 (4 is multiple of 2)
- Frequency of 'y' in s is 1, and in t is 3 (3 is multiple of 1)
- Frequency of 'z' in s is 2, and in t is 2 (same frequency)
- Character 'w' appears in s but not in t (ignored)
Algorithm
To solve this, we will follow these steps ?
- s_freq := a map with all characters in s and their frequencies
- t_freq := a map with all characters in t and their frequencies
- For each character in s_freq, do
- If character not in t_freq, then continue (skip)
- If t_freq[ch] is divisible by s_freq[ch] or s_freq[ch] is divisible by t_freq[ch], then continue
- Otherwise, return False
- Return True
Using Counter from Collections
The most efficient approach uses Python's Counter class to count character frequencies ?
from collections import Counter
def solve(s, t):
s_freq = Counter(s)
t_freq = Counter(t)
for ch in s_freq:
if ch not in t_freq:
continue
if t_freq[ch] % s_freq[ch] == 0 or s_freq[ch] % t_freq[ch] == 0:
continue
else:
return False
return True
s = "xxyzzw"
t = "yyyxxxxzz"
print(solve(s, t))
True
Using defaultdict
Alternative approach using defaultdict to manually count frequencies ?
from collections import defaultdict
def solve_defaultdict(s, t):
s_freq = defaultdict(int)
t_freq = defaultdict(int)
for char in s:
s_freq[char] += 1
for char in t:
t_freq[char] += 1
for ch in s_freq:
if ch not in t_freq:
continue
if t_freq[ch] % s_freq[ch] == 0 or s_freq[ch] % t_freq[ch] == 0:
continue
else:
return False
return True
s = "xxyzzw"
t = "yyyxxxxzz"
print(solve_defaultdict(s, t))
True
Step-by-Step Example
Let's trace through the example with detailed frequency analysis ?
from collections import Counter
def solve_with_details(s, t):
s_freq = Counter(s)
t_freq = Counter(t)
print(f"String s: {s}")
print(f"Frequencies in s: {dict(s_freq)}")
print(f"String t: {t}")
print(f"Frequencies in t: {dict(t_freq)}")
print()
for ch in s_freq:
if ch not in t_freq:
print(f"Character '{ch}' not in string t - skipping")
continue
s_count = s_freq[ch]
t_count = t_freq[ch]
if t_count % s_count == 0:
print(f"Character '{ch}': {t_count} is multiple of {s_count} ?")
elif s_count % t_count == 0:
print(f"Character '{ch}': {s_count} is multiple of {t_count} ?")
else:
print(f"Character '{ch}': {s_count} and {t_count} are not factors ?")
return False
return True
s = "xxyzzw"
t = "yyyxxxxzz"
result = solve_with_details(s, t)
print(f"\nResult: {result}")
String s: xxyzzw
Frequencies in s: {'x': 2, 'y': 1, 'z': 2, 'w': 1}
String t: yyyxxxxzz
Frequencies in t: {'y': 3, 'x': 4, 'z': 2}
Character 'x': 4 is multiple of 2 ?
Character 'y': 3 is multiple of 1 ?
Character 'z': 2 is multiple of 2 ?
Character 'w' not in string t - skipping
Result: True
Conclusion
Use Counter for efficient frequency counting and check if frequencies are factors or multiples using the modulo operator. Characters present in only one string are ignored in the comparison.
