Given a nonempty string s of lowercase English letters, return the character that occurs most often. If multiple characters have the same highest frequency, return the lexicographically smallest one.
The string contains only lowercase English letters ('a' to 'z')
Example 1
Input: s = "takeuforward"
Output: "a"
Explanation: a and r each appear twice, which is the highest frequency. Since a comes before r alphabetically, return a.
Example 2
Input: s = "apple"
Output: "p"
Explanation: p appears twice, which is the highest frequency. Every other character appears once, so return p.
Brute Force Approach
Use an ordered map to store how many times each character appears in the string. Each key represents a character, and its value stores that character’s frequency. When a character appears for the first time, its count becomes 1. Each later occurrence increases that count by 1.
After counting, traverse the map to find the most frequent character. The map visits lowercase letters in alphabetical order. For example, in "rara", both 'r' and 'a' appear twice, and the map visits 'a' first.
Update the answer only when a character has a strictly higher frequency than the best count found so far. In the example, 'a' sets the best count to 2. When 'r' is checked, its count is also 2, so 'a' remains the answer. This rule picks the alphabetically smaller character whenever frequencies tie.
Approach
Create an ordered map to store character frequencies.
Traverse the string and increment the count for each character.
Initialise the best count to
0and declare a variable to store the answer.Traverse the map in alphabetical order. If a character’s count is strictly greater than the best count, save the character and update the best count.
Return the saved character.
Dry Run
Most frequent character in Brute force
Code
#include <bits/stdc++.h>using namespace std;class Solution {public: // Return the most frequent character in a nonempty string. char maxOccurringChar(const string& s) { map<char, int> frequency; for (char ch : s) { ++frequency[ch]; } int bestCount = 0; char answer = s.front(); // The map visits characters in ascending order. for (const auto& entry : frequency) { char ch = entry.first; int count = entry.second; // A strict comparison keeps the smaller character on a tie. if (count > bestCount) { bestCount = count; answer = ch; } } return answer; }};int main() { Solution solution; cout << solution.maxOccurringChar("apple") << '\n'; // Prints p. return 0;}Time and Space Complexity
Let N be the string length and D be the number of distinct characters.
Time Complexity: O(N log(D + 1) + D). Each ordered-map update takes up to logarithmic time in the number of keys, and the final scan visits d entries. Since D ≤ 26 here, the overall time is O(N).
Space Complexity: O(D) auxiliary space, which is O(1) for the fixed lowercase alphabet.
Optimal Approach
Since the string contains only lowercase English letters, we can use an array of size 26 to count how often each letter appears. Index 0 represents 'a', index 1 represents 'b', and so on up to index 25 for 'z'. Subtracting 'a' from a character gives its index. For example, 'p' - 'a' is 15, so every 'p' in "apple" increments frequency[15]. After counting, that position holds 2.
Next, scan the array from left to right to check the letters in alphabetical order. Update the saved index only when a letter’s count is strictly greater than the best count so far. For example, in "rara", both 'a' and 'r' appear twice. The scan reaches 'a' first, and the equal count for 'r' leaves 'a' as the answer.
Approach
Initialise a frequency array of size
26with zeros.Traverse the string. For each character
ch, compute its index withch - 'a'and increment the count at that index.Initialise the best count and the saved index to
0.Scan the array from index
0to25. Whenever the current count is strictly greater than the best count, save both the new count and its index.Convert the saved index back to a letter using
'a' + indexand return it.
Dry Run
Most frequent character Optimal
Code
#include <bits/stdc++.h>using namespace std;class Solution {public: // Return the most frequent lowercase English letter. char maxOccurringChar(const string& s) { const int alphabetSize = 26; int frequency[alphabetSize] = {}; // Subtract 'a' to map each letter to an index from 0 to 25. for (char ch : s) { ++frequency[ch - 'a']; } int bestIndex = 0; // Scan alphabetically and keep the earlier letter when counts tie. for (int i = 1; i < alphabetSize; ++i) { if (frequency[i] > frequency[bestIndex]) { bestIndex = i; } } // Convert the winning index back to a letter. return 'a' + bestIndex; }};int main() { Solution solution; cout << solution.maxOccurringChar("apple") << '\n'; // Prints p. return 0;}Time and Space Complexity
Time Complexity : O(N + 26), which simplifies to O(N). The string is scanned once, followed by a scan of the frequency array.
Space Complexity : O(1) auxiliary space because the array always has 26 entries.
Interview follow-up Questions
Return the lexicographically smallest character. For "zyx", the answer is x.
Be the first to add a comment.