Skip to main content

Posts

Leetcode 77: Combinations

  1. Problem Statement (Simple Explanation) You’re given two integers n and k. You must return  all possible combinations  of k numbers chosen from the range [1, n] (inclusive). Each combination is a set of k distinct numbers. Combinations are  unordered : [1,2] and [2,1] are the same combination; you should only output one of them. You can return the combinations in any order. 2. Examples Example 1: Input: n = 4, k = 2 All 2-number combinations from {1,2,3,4}: [1,2] [1,3] [1,4] [2,3] [2,4] [3,4] Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]] Example 2: Input: n = 1, k = 1 Only one combination: [1]. Output: [[1]] 3. Approach – Backtracking (DFS) We need to generate all subsets of size k from numbers 1..n. Natural approach:  backtracking  (depth-first search): Maintain: current list = current combination being built. start = next number to consider. At each step...

Leetcode 76: Minimum Window Substring

1. Problem Statement (Simple Explanation) You’re given two strings: s (length m) t (length n) You must find the  minimum window substring  of s that  contains every character in t , including duplicates. If no such window exists, return "". It’s guaranteed that if an answer exists, it is  unique . Characters: Both s and t contain only uppercase and lowercase English letters. Goal (follow-up):  O(m + n)  time. 2. Examples Example 1: Input: s = "ADOBECODEBANC", t = "ABC" Valid windows that cover "ABC" include: "ADOBEC" (covers A, B, C) "DOBECODEBA" (longer) "BANC" (B at index 9, A at 10, N at 11, C at 12) The  minimum  such window is "BANC". Output: "BANC" Example 2: Input: s = "a", t = "a" Only window "a". Output: "a" Example 3: Input: s = "a", t = "aa" t requires two 'a's, but s has only one → no valid window. Output: "...