Leetcode 1941: Check if All Characters Have Equal Number of Occurrences

grid47
grid47
Exploring patterns and algorithms
Apr 26, 2024 4 min read

Given a string s, determine if it is a ‘good’ string. A string is considered good if every character in it appears the same number of times.
Problem
Approach
Steps
Complexity
Input: A single string s containing lowercase English letters.
Example: s = "abbcaa"
Constraints:
• 1 <= s.length <= 1000
• s consists of lowercase English letters.
Output: Return true if the string is good, or false otherwise.
Example: Output: true
Constraints:
• Output is a boolean value.
Goal: The goal is to check if all characters in the string have the same frequency.
Steps:
• Count the frequency of each character in the string.
• Check if all the frequencies are the same.
Goal: The length of the string will be between 1 and 1000 characters, and all characters are lowercase English letters.
Steps:
• 1 <= s.length <= 1000
• s consists of lowercase English letters.
Assumptions:
• The string s is non-empty and consists of only lowercase letters.
Input: Example 1: s = "abbcaa"
Explanation: In this case, both 'a', 'b', and 'c' appear twice in the string. Hence, all characters have the same frequency, and the output is true.

Input: Example 2: s = "aabbbcc"
Explanation: In this case, 'a' appears twice, 'b' appears three times, and 'c' appears twice. The frequencies are not all the same, so the output is false.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus