Skip to main content

Posts

Leetcode 49: Group Anagrams

  1. Problem Statement (Simple Explanation): You’re given an array of strings strs. You must  group together all anagrams , where: Two strings are anagrams if one can be  rearranged  to form the other. You can return groups in any order. The order of strings inside each group doesn’t matter. 2. Examples: Example 1: Input: strs = ["eat","tea","tan","ate","nat","bat"] Groups: "bat" → ["bat"] (no anagram partners) "nat", "tan" → ["nat","tan"] "eat", "tea", "ate" → ["eat","tea","ate"] Output: [["bat"],["nat","tan"],["ate","eat","tea"]] Example 2: Input: strs = [""]` Output: [[""]] Example 3: Input: strs = ["a"] Output: [["a"]] 3. Approach – Hash Map with Canonical Key: To group anagrams, we need a way to map ...
Recent posts

Leetcode 48: Rotate Image

  1. Problem Statement (Simple Explanation): You’re given an n x n 2D matrix representing an image. You must  rotate the image by 90° clockwise in-place , meaning: Modify the input matrix directly. Do  not  allocate another n x n matrix. 2. Examples: Example 1: Input: matrix = [   [1,2,3],   [4,5,6],   [7,8,9] ] Output (90° clockwise): [   [7,4,1],   [8,5,2],   [9,6,3] ] Example 2: Input: matrix = [   [5,1,9,11],   [2,4,8,10],   [13,3,6,7],   [15,14,12,16] ] Output: [   [15,13,2,5],   [14,3,4,1],   [12,6,8,9],   [16,7,10,11] ] 3. Approach 1 – Transpose + Reverse Each Row (Clean & Standard): A 90° clockwise rotation can be done in two in-place steps: Transpose the matrix  (swap rows and columns): matrix[i][j] ↔ matrix[j][i] for i < j. After transpose, rows become columns. Reverse...

Leetcode 47: Permutations II

  1. Problem Statement (Simple Explanation): You’re given an array nums that  may contain duplicates . You must return  all unique permutations  of nums. The order of permutations in the output does not matter. 2. Examples: Example 1: Input: nums = [1,1,2] All permutations (with duplicates): [1,1,2] [1,2,1] [1,1,2] (duplicate) [1,2,1] (duplicate) [2,1,1] [2,1,1] (duplicate) Unique permutations: [   [1,1,2],   [1,2,1],   [2,1,1] ] Example 2: Input: nums = [1,2,3] All permutations are unique: [   [1,2,3],   [1,3,2],   [2,1,3],   [2,3,1],   [3,1,2],   [3,2,1] ] 3. Approach – Backtracking with Duplicate Handling: This is similar to Problem 46 (Permutations) but nums may contain duplicates, so naive permutation will produce duplicate lists. We need to  avoid generating duplicates  instead of removing them afterwards. Standard trick: sor...