Skip to main content

Posts

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: "...

List of Lists

You’re given: An integer  N  and an array  A  of size  N . Initially you have  N  lists: L i =[ A i ] . Operation (allowed while at least 2 lists exist): Pick two  non-empty  lists  L i ,  L j  with  i≠j . Append  L j  to  L i  (concatenate), then delete  L j  from the set of lists. Goal: Obtain a final set of lists such that: For every list,  first element = last element . The  first element of all lists is the same . Minimize the number of operations. If impossible, print -1. Key Observations First and last element equal in each list After all operations, each list must look like: [x, …, x] (first and last element both  x ), possibly of length 1. Same first element across lists All lists must start with the same value, say  X . So any list whose first element is not  X  in the final state must be  merged into a list starting with  X Length-1 lists...