githubEdit

06. Smallest Window Containing All Characters

βœ… GFG solution to Smallest Window Containing All Characters: find minimum window in string containing all characters of another string using sliding window technique. πŸš€

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

🧩 Problem Description

Given two strings s and p, find the smallest substring in s consisting of all the characters (including duplicates) of the string p. Return empty string in case no such substring is present. If there are multiple such substrings of the same length, return the one with the least starting index.

πŸ“˜ Examples

Example 1

Input: s = "timetopractice", p = "toc"
Output: "toprac"
Explanation: "toprac" is the smallest substring in which "toc" can be found.

Example 2

Input: s = "zoomlazapzo", p = "oza"
Output: "apzo"
Explanation: "apzo" is the smallest substring in which "oza" can be found.

Example 3

Input: s = "zoom", p = "zooe"
Output: ""
Explanation: No substring is present containing all characters of p.

πŸ”’ Constraints

  • $1 \le \text{s.length()}, \text{p.length()} \le 10^6$

  • s, p consists of lowercase English letters

βœ… My Approach

The optimal solution uses Sliding Window with Character Frequency Tracking:

Two-Pointer Sliding Window

  1. Frequency Tracking:

    • Store frequency of all characters in pattern p.

    • Track frequency of characters in current window of s.

    • Count how many required characters are satisfied.

  2. Window Expansion:

    • Move right pointer to expand window.

    • Add character to window frequency.

    • If character is needed and not over-counted, increment match count.

  3. Window Contraction:

    • When all characters are matched, try to minimize window.

    • Move left pointer while window still contains all characters.

    • Remove characters that are not needed or are in excess.

  4. Result Tracking:

    • Track minimum window length and starting position.

    • Update when a smaller valid window is found.

    • Return substring if valid window exists, else empty string.

Key Insight: Use character frequency arrays for O(1) character operations. Match count tracks when all required characters are present in current window.

πŸ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n + m), where n is the length of string s and m is the length of string p. We make at most two passes through s (right pointer and left pointer), and one pass through p to build frequency map.

  • Expected Auxiliary Space Complexity: O(1), as we use fixed-size arrays of 256 elements (for all ASCII characters) or 26 elements (for lowercase letters only), which is constant space regardless of input size.

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

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

chevron-right⚑ View Alternative Approaches with Code and Analysishashtag

πŸ“Š 2️⃣ Hash Map Approach

πŸ’‘ Algorithm Steps:

  1. Use unordered_map instead of fixed arrays for character frequencies.

  2. Track required character count and formed character count.

  3. Expand window until all characters found, then contract.

  4. Update minimum window when valid window found.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n + m) - Linear time with hash operations

  • Auxiliary Space: πŸ’Ύ O(k) - Where k is unique characters in p

βœ… Why This Approach?

  • Flexible for any character set (not just ASCII)

  • Clear separation of required vs formed counts

  • Easy to extend for additional constraints

πŸ“Š 3️⃣ Optimized Array with Match Counting

πŸ’‘ Algorithm Steps:

  1. Use array of size 128 for ASCII characters.

  2. Track total matches needed and current matches.

  3. Slide window with efficient match tracking.

  4. Return smallest valid window found.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n + m) - Single pass through both strings

  • Auxiliary Space: πŸ’Ύ O(1) - Fixed size array

βœ… Why This Approach?

  • Most compact code

  • Efficient counter-based matching

  • Optimal for ASCII characters

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

🎯 Array Sliding Window

🟒 O(n + m)

🟒 O(1)

πŸš€ Fastest, constant space

πŸ”§ Limited to ASCII/lowercase

πŸ” Hash Map

🟒 O(n + m)

🟑 O(k)

πŸ“– Flexible for any charset

πŸ”§ Hash map overhead

πŸ“Š Match Counter

🟒 O(n + m)

🟒 O(1)

⚑ Most compact code

πŸ”§ Requires understanding counters

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

πŸ… Optimal performance needed

πŸ₯‡ Array Sliding Window

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

πŸ“– Any character set

πŸ₯ˆ Hash Map

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

⚑ Most compact solution

πŸ₯‰ Match Counter

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

β˜• 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