Skip to main content

Posts

Leetcode 31: Next Permutation

  1. Problem Statement (Simple Explanation): You’re given an array of integers nums representing a permutation. You must rearrange nums  in-place  to form the  next lexicographically greater permutation . If such a permutation exists: transform nums into it. If not (i.e., nums is the highest permutation), rearrange nums into the  lowest possible order  (sorted ascending). Constraints: In-place (only constant extra memory). 1 <= nums.length <= 100 0 <= nums[i] <= 100 2. Examples: Example 1: Input: nums = [1,2,3] Permutations in order: [1,2,3] [1,3,2]  <-- next [2,1,3] [2,3,1] [3,1,2] [3,2,1] Output: [1,3,2] Example 2: Input: nums = [3,2,1] This is the  largest  permutation. The next permutation wraps to the  smallest : Output: [1,2,3] Example 3: Input: nums = [1,1,5] Next permutations: [1,1,5] [1,5,1]  <-- next [5,1,1] Output: [1,5,1] 3. Intuition – Classic Next Permu...
Recent posts

Leetcode 30: Substring with Concatenation of All Words

1. Problem Statement (Simple Explanation): You are given: A string s. An array of strings words, where: All words[i] have the  same length . A  concatenated string  in this problem is: A string formed by concatenating  all  the words in words in  any order , using each word  exactly as many times as it appears in words  (i.e., including duplicates). You must: Find  all starting indices  i in s such that the substring: s[i .. i + totalLen - 1] is a concatenated string of all words (in some permutation). Return the list of indices (order doesn’t matter). Where: wordLen = length(words[0]) wordCount = len(words) totalLen = wordLen * wordCount 2. Examples: Example 1: Input: s = "barfoothefoobarman" words = ["foo","bar"] wordLen = 3 wordCount = 2 totalLen = 6 Valid substrings: Starting at index 0: "barfoo" → "bar" + "foo" (a permutation of ["foo","bar"]) Start...