Given a string s
, return true
if s
is a good string, or false
otherwise.
A string s
is good if all the characters that appear in s
have the same number of occurrences (i.e., the same frequency).
Example 1:
Input: s = "abacbc" Output: true Explanation: The characters that appear in s are 'a', 'b', and 'c'. All characters occur 2 times in s.
Example 2:
Input: s = "aaabb" Output: false Explanation: The characters that appear in s are 'a' and 'b'. 'a' occurs 3 times while 'b' occurs 2 times, which is not the same number of times.
Constraints:
1 <= s.length <= 1000
s
consists of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To see if every letter appears the same number of times, we first need to count how many times each unique letter shows up. Then, we can simply compare the count of the first letter to the count of every other letter to see if they are all the same.
Here's how the algorithm would work step-by-step:
def are_occurrences_equal(input_string):
if not input_string:
return True
unique_characters = list(set(input_string))
# Pick the first character's count as the reference to compare all others against.
first_character = unique_characters[0]
reference_occurrence_count = input_string.count(first_character)
# Iterate through the rest of the unique characters to check their counts.
for character_index in range(1, len(unique_characters)):
current_character = unique_characters[character_index]
current_character_count = input_string.count(current_character)
# If any character count differs, the condition is not met, so we can exit early.
if current_character_count != reference_occurrence_count:
return False
# If all character counts matched the reference count, the condition is met.
return True
The core idea is to first count how many times each unique character appears in the text. Then, we simply check if all those counts are the same number.
Here's how the algorithm would work step-by-step:
def are_occurrences_equal(input_string):
character_to_count_map = {}
# Step 1: Tally up the occurrences for every distinct character.
for character in input_string:
character_to_count_map[character] = character_to_count_map.get(character, 0) + 1
# Step 2: Establish a reference count from the first character's frequency.
# This is the target value that all other character counts must match.
reference_occurrence_count = character_to_count_map[input_string[0]]
# Step 4 & 5: Compare each character's count to the reference number.
# If any count differs, we can immediately conclude the condition is not met.
for character_count in character_to_count_map.values():
if character_count != reference_occurrence_count:
return False
# Step 6: If all counts match the reference, the condition is met.
return True
Case | How to Handle |
---|---|
Empty string | The solution should return true as the condition of all appearing characters having the same frequency is vacuously met. |
Null or undefined input string | The code should handle this gracefully, typically by throwing an error or returning false depending on the requirements. |
String with a single character | This is a base case that should return true, as there is only one character type with a single frequency. |
String with all identical characters | The solution correctly returns true because only one character type exists, so there's only one frequency to compare. |
String with only one of each character | This case should return true since every character appears exactly once, making all frequencies equal to 1. |
String with different character casings, e.g., 'aA' | The solution must treat uppercase and lowercase letters as distinct characters unless the problem specifies otherwise. |
String containing non-alphanumeric characters like spaces, punctuation, or symbols | These characters should be counted just like letters, each contributing to their own frequency count. |
Very long string approaching memory or time limits | The frequency map approach has a time complexity of O(N) and space complexity of O(K), which scales efficiently for large inputs. |