githubEdit

11. Minimum Window Subsequence

βœ… GFG solution to the Minimum Window Subsequence problem: find the smallest substring in s1 that contains s2 as a subsequence using greedy backtracking and two-pointer techniques. πŸš€

The problem can be found at the following link: πŸ”— Question Linkarrow-up-right

🧩 Problem Description

You are given two strings, s1 and s2. Your task is to find the smallest substring in s1 such that s2 appears as a subsequence within that substring.

  • The characters of s2 must appear in the same sequence within the substring of s1.

  • If there are multiple valid substrings of the same minimum length, return the one that appears first in s1.

  • If no such substring exists, return an empty string.

Note: Both the strings contain only lowercase English letters.

πŸ“˜ Examples

Example 1

Input: s1 = "geeksforgeeks", s2 = "eksrg"
Output: "eksforg"
Explanation: "eksforg" satisfies all required conditions. s2 is its subsequence and it is 
smallest and leftmost among all possible valid substrings of s1.

Example 2

Input: s1 = "abcdebdde", s2 = "bde" 
Output: "bcde"
Explanation: "bcde" and "bdde" are two substrings of s1 where s2 occurs as subsequence 
but "bcde" occurs first so we return that.

Example 3

πŸ”’ Constraints

  • $1 \le \text{s1.length} \le 10^4$

  • $1 \le \text{s2.length} \le 50$

βœ… My Approach

The optimal approach uses a Greedy Backtracking with Two-Pointer technique to efficiently find the minimum window subsequence:

Greedy Backtracking Strategy

  1. Forward Pass - Find Complete Subsequence:

    • Use two pointers: i for s1 and j for s2.

    • Move i forward through s1, incrementing j whenever characters match.

    • When j reaches the end of s2, we've found a valid window endpoint.

  2. Backward Pass - Minimize Window:

    • Once a complete subsequence is found, backtrack from the current position.

    • Move i backward while decrementing j to find the leftmost starting position.

    • This gives us the minimum window for this particular endpoint.

  3. Track Minimum Window:

    • Compare the current window length with the global minimum.

    • Update the result if a smaller window is found.

    • Continue the forward pass to explore other potential windows.

  4. Optimization:

    • After each backtrack, resume the forward search from the optimized starting position.

    • This ensures we don't miss any potentially smaller windows.

πŸ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(m Γ— n), where m is the length of s1 and n is the length of s2. In the worst case, for each character in s1, we might backtrack through the entire subsequence s2.

  • Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for pointers and variables, regardless of the input size.

πŸ§‘β€πŸ’» Code (C++)

chevron-right⚑ View Alternative Approaches with Code and Analysishashtag

πŸ“Š 2️⃣ Dynamic Programming Approach

πŸ’‘ Algorithm Steps:

  1. Build a DP table storing the starting index of each character match.

  2. For each position, track where the subsequence starting point is.

  3. Iterate through all valid endpoints to find minimum window.

  4. Return the smallest substring containing the subsequence.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(m Γ— n) - Fill entire DP table

  • Auxiliary Space: πŸ’Ύ O(m Γ— n) - 2D DP array storage

βœ… Why This Approach?

  • Systematic tabulation of all possibilities

  • Clear state transitions and relationships

  • Works well for understanding the problem structure

πŸ“Š 3️⃣ Next Index Tracking

πŸ’‘ Algorithm Steps:

  1. Precompute next occurrence of each character from every position.

  2. For each starting position, greedily find the closest match.

  3. Track the minimum window length during traversal.

  4. Use next index array for O(1) character lookups.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(m Γ— n) - Preprocess and search

  • Auxiliary Space: πŸ’Ύ O(26m) - Next index arrays

βœ… Why This Approach?

  • Efficient character lookup with preprocessing

  • Good for multiple queries on same string

  • Optimized for alphabet size consideration

πŸ“Š 4️⃣ Two-Pointer Sliding Window

πŸ’‘ Algorithm Steps:

  1. Use two pointers to expand and contract the window.

  2. Expand right pointer to include all characters of s2.

  3. Contract left pointer to minimize window size.

  4. Track minimum valid window throughout the process.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(m Γ— n) - Worst case backtracking

  • Auxiliary Space: πŸ’Ύ O(1) - Constant extra space

βœ… Why This Approach?

  • Space efficient with minimal memory usage

  • Natural sliding window pattern application

  • Intuitive expand and contract logic

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

🏷️ Greedy Backtrack

🟒 O(m Γ— n)

🟒 O(1)

πŸš€ Optimal space usage

πŸ”§ Complex backtracking logic

πŸ” Dynamic Programming

🟑 O(m Γ— n)

🟑 O(m Γ— n)

πŸ“– Clear state transitions

πŸ’Ύ High memory usage

πŸ“Š Next Index Tracking

🟑 O(m Γ— n)

🟑 O(26m)

🎯 Fast character lookup

πŸ’Ύ Extra preprocessing needed

πŸ”„ Two-Pointer Sliding

🟑 O(m Γ— n)

🟒 O(1)

⭐ Intuitive window logic

🐌 May revisit positions

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

πŸ… Memory-constrained systems

πŸ₯‡ Greedy Backtrack

β˜…β˜…β˜…β˜…β˜…

πŸ“– Learning DP patterns

πŸ₯ˆ Dynamic Programming

β˜…β˜…β˜…β˜…β˜†

πŸ”§ Multiple queries on same text

πŸ₯‰ Next Index Tracking

β˜…β˜…β˜…β˜…β˜†

🎯 Interview scenarios

πŸ… Two-Pointer Sliding

β˜…β˜…β˜…β˜…β˜…

β˜• Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: πŸ“¬ Any Questions?arrow-up-right. Let's make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Visitor counter

Last updated