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
Max Frequency Character in String: O(n) Solution
More Relevant Posts
-
🚀 Day 66 of #100DaysOfCode Today, I solved LeetCode 73 – Set Matrix Zeroes, a classic problem that tests in-place matrix manipulation and optimization techniques. 💡 Problem Overview: Given a matrix, if any cell contains 0, its entire row and column must be set to 0. The challenge is to perform this efficiently without using excessive extra space. 🧠 Approach: To solve this optimally, I focused on: ✔️ Using the first row and first column as markers ✔️ Tracking whether the first row/column initially contained zero ✔️ Updating the matrix in-place based on these markers This avoids using additional space and achieves optimal performance. ⚡ Key Takeaways: In-place algorithms help reduce space complexity Using matrix itself as storage is a powerful optimization trick Handling edge cases (first row/column) is critical 📊 Complexity Analysis: Time Complexity: O(n × m) Space Complexity: O(1) Improving problem-solving skills one day at a time 🚀 #LeetCode #100DaysOfCode #DSA #Matrix #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep
To view or add a comment, sign in
-
-
Day 33 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Longest Substring with Distinct Characters We were given a string s. Task was to find the length of the longest substring with all **unique characters**. Example: "geeksforgeeks" → 7 Substring → "eksforg" --- 💻 Approach (Sliding Window) 🔹️Use two pointers: start and end. 🔹️Maintain a set to store characters in current window. 🔹️Expand the window by moving end. 🔹️If character is not in set → add it. 🔹️If duplicate appears → remove from start side. 🔹️Keep updating maximum length. Dynamic window. No need to check all substrings. --- 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(1) (At most 26 characters stored) --- 📚 What I learned today: ▫️Sliding window is best for substring problems. ▫️Set helps in checking duplicates quickly. ▫️Window expansion and shrinking must be balanced. ▫️Avoiding brute force saves a lot of time. Day 33 completed. This one improved my sliding window understanding 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
Day 44 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Check Palindrome Using Recursion We were given a string s. Task was to check whether it is a palindrome. But using recursion. 💻 Approach (Recursion + Two Pointers) 🔹️Take two pointers: start and end. 🔹️Base case: ▪️If start >= end → return true 🔹️Compare characters at start and end. 🔹️If they are not equal → return false 🔹️If equal → move inward ▪️start + 1 ▪️end - 1 🔹️Call function again for inner substring Keep checking till center. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) Due to recursion stack. 📚 What I learned today: ▫️Recursion can simulate two-pointer traversal. ▫️Base condition defines when to stop checking. ▫️Breaking problem into smaller substrings simplifies logic. ▫️Recursive calls use stack space internally. Day 44 completed. Combining recursion with pointers 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
Day 45 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Check Palindrome Using Recursion We were given a string s. Task was to check whether it is a palindrome. Using recursion. --- 💻 Approach (Recursion + Two Pointers) 🔹️Take two pointers: start and end. 🔹️Base case: ▪️If start >= end → return true 🔹️Compare characters at start and end. 🔹️If not equal → return false 🔹️If equal → move inward ▪️start + 1 ▪️end - 1 🔹️Call function again for inner substring Keep checking until pointers meet. --- 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) Due to recursion stack. --- 📚 What I learned today: ▫️Recursion can replicate iterative two-pointer logic. ▫️Each recursive call reduces problem size by 2. ▫️Base condition ensures termination at center. ▫️Space complexity increases due to call stack. Day 44 completed. Recursion patterns getting stronger 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
Day 9 of #30DaysOfLeetCode Solved #27 – Remove Element on LeetCode using C. Problem: Given an integer array nums and a value val, remove all occurrences of val in-place and return the number of elements that are not equal to val. The first k elements of the array should contain the remaining valid elements. Approach: • Used the Two Pointer Technique • Traversed the array with index i • Maintained another pointer k to track valid elements • If nums[i] != val, copied the element to nums[k] • Incremented k to store the next valid element • This shifts all non-val elements to the front of the array Performance: • Runtime: 0 ms (Beats 100% ) • Memory: 10.33 MB Key Learnings: • Practical understanding of the Two Pointer technique • Performing in-place array modification • Writing efficient O(n) time and O(1) space solutions • Strengthening fundamentals of array manipulation #LeetCode #DSA #Algorithms #CProgramming #ProblemSolving
To view or add a comment, sign in
-
-
Day 36 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Uncommon Characters in Two Strings We were given two strings s1 and s2. Task was to find characters that are present in only one string. Not in both. Final result should be sorted 💻 Approach 🔹️Create two frequency arrays (or sets). 🔹️Traverse s1 and mark its characters. 🔹️Traverse s2 and mark its characters. 🔹️Now check each character from 'a' to 'z': ▪️If present in only one string → include it ▪️If present in both → skip it 🔹️Add valid characters to result string. 🔹️Sort the result. Simple comparison logic. --- 📊 Complexity Analysis Time Complexity: **O(n + m)** Space Complexity: **O(n + m)** --- 📚 What I learned today: ▫️Set/frequency based comparison is useful for string problems. ▫️Checking presence instead of count simplifies logic. ▫️Iterating over fixed character range helps in sorting automatically. ▫️Problem is more about filtering than processing. Day 36 completed. Understanding string comparison better 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
Day 30 of Solve With Me: Today’s problem: Reverse String (LeetCode 344). We are given a character array s. Our task is to reverse the array in-place (without using extra space). What is the idea? We swap characters from the beginning and end moving toward the center. Approach (Two Pointer ) Initialize two pointers: start = 0 end = s.length - 1 Traverse till the middle: Swap s[start] and s[end] Increment start Decrement end Stop when start >= end Why this works ? Each swap places characters in their correct reversed position. Only half traversal is needed since we fix two positions in one step. Example: Input : s = ['h','e','l','l','o'] Step 1 : swap h ↔ o → ['o','e','l','l','h'] Step 2 : swap e ↔ l → ['o','l','l','e','h'] Output : ['o','l','l','e','h'] #leetcode #problemsolving #dsa #codingpractice #day30
To view or add a comment, sign in
-
-
🚀 Day 68 of #100DaysOfCode 🔥 Solved Median of Two Sorted Arrays (LeetCode Q4) today! 💡 Problem Insight: Find the median of two sorted arrays in an efficient way without merging them. 🧠 Approach: - Use Binary Search on the smaller array - Partition both arrays such that left side has smaller elements and right side has larger ones - Ensure correct balance of elements on both sides ⚙️ Algorithm: - Apply binary search to find correct partition - Check conditions for valid split - If valid → calculate median based on total length - Else → adjust search space ⏱️ Complexity: - Time: O(log(min(n, m))) - Space: O(1) 📌 Key Learning: Brute force is not always the answer — smart partitioning can optimize everything. 💬 Think smart, not hard. #DSA #LeetCode #BinarySearch #100DaysOfCode #CodingJourney #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 37 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Second Most Repeated String in a Sequence We were given a list of strings. Task was to find the second most frequent string. Not the most frequent. The one just below it. 💻 Approach 🔹️Create a hash map to store frequency of each string. 🔹️Traverse the sequence and count occurrences. 🔹️Track the highest and second highest frequency. 🔹️Find the string with second highest count. 🔹️Return that string. Simple counting + comparison. 📊 Complexity Analysis Time Complexity: O(N * max(|Si|)) Space Complexity: O(N * max(|Si|)) 📚 What I learned today: ▫️Hash maps are very useful for frequency-based problems. ▫️Tracking second maximum needs careful comparison. ▫️One-pass counting + second pass evaluation works well. ▫️String problems often reduce to counting patterns. Day 37 completed. Getting more comfortable with hash maps 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
🚀 Day 10 of Coding Solved Problem #844 – Backspace String Compare 💡 🔍 Problem Statement: Given two strings s and t, return true if they are equal when both are typed into empty text editors. # means a backspace character. 🧠 Key Learning: ✔ Mastered two-pointer approach from the end ✔ Learned how to handle backspace operations efficiently ✔ Avoided extra space by not building new strings ⚡ Approach: 👉 Traverse both strings from right to left 👉 Use counters to skip characters affected by # 👉 Compare valid characters one by one 👉 Return false if mismatch occurs 💻 Tech Stack: C++ | STL ✅ Result: ✔ Accepted ✔️ ✔ Optimized with O(n) time and O(1) space 🔥 Another step forward in consistency and problem-solving! #Day10 #LeetCode #DSA #CodingJourney #CPP #ProblemSolving
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development