Simple 2 pointer approach || Beats 100% 🔥🔥 || Python &  CPP || T.C. : O(n) & S.C. : O(1)
IntuitionThe problem is about finding the smallest substring of s that contains all the characters of string t (including duplicates). The task can be efficient
![Screenshot 2025-01-26 001905.png](https://assets.leetcode.com/users/images/ad26705f-1343-43ea-b9eb-d5fdc4dab23f_1737831087.9904838.png)

---

# Intuition
The problem is about finding the smallest substring of s that contains all the characters of string t (including duplicates). The task can be efficiently solved using the sliding window technique with a frequency map to count characters.

The goal is to:
1. Expand the window to include characters until all characters of t are covered.
2. Shrink the window while maintaining the condition that all characters of t are still covered to minimize the substring length.

---

# Approach
1. Frequency Map for t:
• Create a frequency map (Hash) to store the count of each character in t. This helps us keep track of which characters and how many of each are still needed.

2. Sliding Window:
• Use two pointers (l and r) to define the current window in s.
• Expand the window by moving the right pointer (r) and updating the frequency map for the characters in the current window.
• Maintain a count (cnt) of how many characters from t are currently satisfied in the window.

3. Check for Valid Window:
• When all characters of t are covered (cnt == len(t)), try shrinking the window from the left (l) to see if a smaller valid window exists.
• If the current window is smaller than the previously recorded minimum window, update the result.

4. Move Left Pointer:
• Increment the count of the character at l in the frequency map.
• If it causes a character from t to fall below its required count, decrement cnt.

5. Output the Result:
• After iterating through s, return the smallest valid window substring. If no such substring exists, return an empty string.

---

# Complexity
1. Time complexity:
• Building the Frequency Map: O(m), where m is the length of t.
• Sliding Window Process: The right pointer (r) iterates through the string s once, and the left pointer (l) moves independently. Both pointers combined move at most  times, where n is the length of s. Thus, this part is O(n).
• Overall: (n+m).

2. Space complexity:
• Frequency Map: A fixed size array of size 256 is used to store character frequencies. This takes O(1) space.
• Additional Variables: A few integers are used for tracking indices and counts.
• Overall: O(1) additional space.

---

# Code
```python3 []
# If it's helpful, Please upvote my solution🙏.
class Solution:
    def minWindow(self, s: str, t: str) -> str:
        n, m = len(s), len(t)
        l, r, cnt = 0, 0, 0
        maxLen = 10**9
        sIndex = n
        Hash = [0]*256

        for c in t:
            Hash[ord(c)] += 1
        
        while(r < n):
            if Hash[ord(s[r])] > 0: cnt += 1
            Hash[ord(s[r])] -= 1
            while(cnt == m):
                if (r-l+1) < maxLen:
                    maxLen = r -l+1
                    sIndex = l
                Hash[ord(s[l])] += 1
                if Hash[ord(s[l])] > 0: cnt -= 1
                l += 1
            r += 1
        return s[sIndex:sIndex+maxLen]
```
```cpp []
class Solution {
public:
    string minWindow(string s, string t) {
        int n = s.length(), m = t.length();
        int l = 0, r = 0, cnt = 0;
        int maxLen = INT_MAX;
        int sIndex = n;
        vector<int> hash(256, 0);

        for (char c : t) {
            hash[c]++;
        }

        while (r < n) {
            if (hash[s[r]] > 0) cnt++;
            hash[s[r]]--;
            while (cnt == m) {
                if (r - l + 1 < maxLen) {
                    maxLen = r - l + 1;
                    sIndex = l;
                }
                hash[s[l]]++;
                if (hash[s[l]] > 0) cnt--;
                l++;
            }
            r++;
        }

        return maxLen == INT_MAX ? "" : s.substr(sIndex, maxLen);
    }
};
```

---

![WhatsApp Image 2025-01-24 at 20.52.43.jpeg](https://assets.leetcode.com/users/images/ac5a8003-704e-4fcb-ad04-376a21bf42c7_1737831686.889867.jpeg)

['Hash Table', 'Two Pointers', 'Sliding Window', 'C++', 'Python3']