Illustration showing how strings are sorted and grouped together as anagrams using a hashmap

How to Group Anagrams: O(n · m log m) Solution Explained

Problem Overview We are given an array of strings strs Our goal is to group the anagrams together and return an array of array of strings All strings are in lowercase English letters Leetcode - Group Anagrams Example Input: strs = ["eat","tea","tan","ate","nat","bat"] Output: [["bat"],["nat","tan"],["ate","eat","tea"]] nat and tan are anagrams ate, eat and tea are anagrams There is no anagram for bat in the array If you dont know how to find anagrams then refer this Valid Anagrams ...

March 13, 2026 · 3 min · 436 words · Hitesh Patel
Illustration showing how two strings are checked to determine if they are anagrams

How to find if string is Valid Anagram: O(n) solution explained

Problem Overview We are given s and t of length m and n respectively We need to check if t is a valid anagram of s or not. All characters will be in lowercase. Note — An anagram is a word or phrase that is built by rearranging the characters of another word in a different order to form a new word. Example Input: s = "anagram", t = "nagaram" Output: true Input: s = "rat", t = "car" Output: false Brute Force Intuition If t and s are not of the same size, then they cannot be valid anagrams. If all the characters present in s are present in t with the same frequency, then it is a valid anagram. Use an array to store the frequency of characters from s. Then iterate over t and decrease the frequency. Finally iterate over the array — if anything other than 0 is present, then it is invalid. Code class Solution { public: bool isAnagram(string s, string t) { if(s.size() != t.size()) return false; vector<int> freq = vector<int>(26, 0); for(auto it: t){ freq[it-'a']++; } for(auto it: s){ freq[it-'a']--; } for(auto it: freq){ if(it != 0) return false; } return true; } }; Time & Space Complexity Time - O(n) Space - O(26) → O(1) Optimization Intuition Instead of checking validity in a separate loop, we can decrease the frequency and check it in the same loop. To further optimize, we can use a fixed-size array instead of a vector. Code class Solution { public: bool isAnagram(string s, string t) { if(s.size() != t.size()) return false; int freq[26] = {0}; for(char c : s) freq[c-'a']++; for(char c : t) if(--freq[c-'a'] < 0) return false; return true; } }; Time & Space Complexity Time - O(n) Space - O(26) → O(1) Key Takeaways You need to understand what an anagram is before solving the problem. Observe and try to reduce unnecessary iterations if possible.

March 12, 2026 · 2 min · 321 words · Hitesh Patel
Illustration showing sliding window technique to find minimum window substring

Minimum Window Substring: Sliding Window Explained

Problem Overview Given strings s and t of length m and n respectively, return the minimum length substring of s such that every character in t is present in the substring. The substring can contain duplicate characters. LeetCode - Rotate Image Example: Input: s = "ADOBECODEBANC", t = "ABC" Output: "BANC" BANC is the minimum window containing all characters from t. ...

March 11, 2026 · 3 min · 504 words · Hitesh Patel
Illustration showing sliding window technique to find longest repeating character replacement

Longest Repeating Character Replacement: Sliding Window Explained

Problem Overview You’re given a string s (uppercase letters only) and an integer k. You can replace any character in the string up to k times. Return the length of the longest substring containing the same letter after performing those replacements. Brute Force Intuition The idea is straightforward — generate every possible substring, track the frequency of each character inside it, and check if the window is “valid.” Try all possible substrings with two nested loops Track character frequency within the current substring Track maxFreq — the count of the most frequent character If len - maxFreq <= k, the window is valid — update the result How this works, we have the maxFreq of that substring if we subract it from the len then we got len of other chars. The string is only valid if the no of conversion we have to make is less than k that is length - maxFreq; ...

March 10, 2026 · 3 min · 563 words · Hitesh Patel
Illustration showing sliding window technique to find longest substring without repeating characters

Longest Substring Without Repeating Characters: Sliding Window Explained

Problem Overview We are given a string s. Our goal is to find the longest substring that does not contain repeating characters. In other words, the length of the substring should equal the number of unique characters in it. If found → return the length, otherwise → return 0. There can be multiple valid substring of length LeetCode - Longest Substring Without Repeating Characters A substring is continous part of string. The string itself is a valid substring. ...

March 9, 2026 · 3 min · 637 words · Hitesh Patel