Max Frequency Character in String: O(n) Solution

Day 32 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Most Frequent Character in a String We were given a string s. All characters are lowercase. Task was to find the character with maximum frequency. If multiple characters have same frequency, return the lexicographically smaller one. Example: "testsample" → e 💻 Approach 🔹️Create a frequency array of size 26. 🔹️Traverse the string and count each character. 🔹️Find the character with maximum frequency. 🔹️If frequencies are same, pick smaller character. Simple counting logic. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(1) Fixed array of size 26. 📚 What I learned today: ▫️Frequency arrays are very useful for string problems. ▫️Handling tie conditions (lexicographic order) is important. ▫️Constant space solutions are possible with fixed character sets. ▫️Careful comparison logic matters in final result. Day 32 completed. Improving string handling step by step 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic

To view or add a comment, sign in

Explore content categories