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 max occurring character of one string appears same no. of times in other in Python
When working with string analysis, we sometimes need to check if the most frequently occurring character in one string appears the same number of times in another string. This can be useful in text analysis and pattern matching scenarios.
Problem Statement
Given two strings s and t, we need to find the most frequent character in string s and check whether that character appears the same number of times in string t.
For example, if s = "crosssection" and t = "securesystem", the most frequent character in s is 's' (appears 3 times). Since 's' also appears 3 times in t, the result is True.
Algorithm Steps
- Create a frequency map for all characters in string
s - Find the character with maximum frequency in
s - Count occurrences of this character in string
t - Compare the frequencies and return
Trueif they match,Falseotherwise
Implementation
from collections import defaultdict
def solve(s, t):
# Create frequency map for string s
freq = defaultdict(int)
for char in s:
freq[char] += 1
# Find character with maximum frequency
max_freq_char = max(freq, key=freq.get)
max_freq = freq[max_freq_char]
# Check if same character appears same number of times in t
if max_freq == t.count(max_freq_char):
return True
return False
# Test the function
s = "crosssection"
t = "securesystem"
result = solve(s, t)
print(f"String s: {s}")
print(f"String t: {t}")
print(f"Result: {result}")
String s: crosssection String t: securesystem Result: True
Step-by-Step Analysis
Let's trace through the example to understand how it works:
from collections import defaultdict
def analyze_strings(s, t):
# Count frequencies in string s
freq = defaultdict(int)
for char in s:
freq[char] += 1
print(f"Character frequencies in '{s}':")
for char, count in sorted(freq.items()):
print(f" '{char}': {count}")
# Find most frequent character
max_freq_char = max(freq, key=freq.get)
max_freq = freq[max_freq_char]
print(f"\nMost frequent character: '{max_freq_char}' (appears {max_freq} times)")
# Count occurrences in string t
count_in_t = t.count(max_freq_char)
print(f"Character '{max_freq_char}' appears {count_in_t} times in '{t}'")
return max_freq == count_in_t
s = "crosssection"
t = "securesystem"
result = analyze_strings(s, t)
print(f"\nResult: {result}")
Character frequencies in 'crosssection': 'c': 2 'e': 1 'i': 1 'n': 2 'o': 2 'r': 1 's': 3 't': 1 Most frequent character: 's' (appears 3 times) Character 's' appears 3 times in 'securesystem' Result: True
Alternative Implementation Using Counter
We can also use Python's Counter class for a more concise solution:
from collections import Counter
def solve_with_counter(s, t):
# Count character frequencies in s
counter_s = Counter(s)
# Find most common character and its frequency
most_common_char, max_freq = counter_s.most_common(1)[0]
# Check frequency in t
return Counter(t)[most_common_char] == max_freq
# Test with multiple examples
test_cases = [
("crosssection", "securesystem"),
("hello", "world"),
("aabbcc", "abcabc")
]
for s, t in test_cases:
result = solve_with_counter(s, t)
print(f"s='{s}', t='{t}' ? {result}")
s='crosssection', t='securesystem' ? True s='hello', t='world' ? True s='aabbcc', t='abcabc' ? False
Conclusion
This algorithm efficiently finds the most frequent character in one string and compares its frequency with the same character in another string. The Counter approach provides a cleaner implementation, while the manual frequency counting offers more control over the process.
