4. Smallest Distinct Window
The problem can be found at the following link: 🔗 Question Link
🧩 Problem Description
Given a string str, find the length of the smallest window (substring) that contains all the distinct characters of the string at least once.
📘 Examples
Example 1:
Input:
str = "aabcbcdbca"
Output:
4
Explanation:
The smallest window that contains all characters is "dbca".
Example 2:
Input:
str = "aaab"
Output:
2
Explanation:
The smallest window that contains all distinct characters is "ab".
Example 3:
Input:
str = "geeksforgeeks"
Output:
8
Explanation:
Substrings "geeksfor" and "forgeeks" are both valid.
🔒 Constraints
$1 \leq \text{str.length} \leq 10^5$
strcontains only lowercase English letters.
✅ My Approach
Sliding Window + Frequency Table
This is a classic sliding window problem where we dynamically maintain a window that tries to include all unique characters, and then shrink it from the left as much as possible while still retaining all distinct characters.
Algorithm Steps:
Compute the number of distinct characters in the string
str. Let this bed.Use a sliding window from two pointers
iandj.Use a frequency array of size 256 to count characters in the window.
Keep expanding the window by moving
jand updating character counts.When the current window includes all
dcharacters, try to shrink it by movingi.Keep track of the minimum window length during this process.
🧮 Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), as we process each character at most twice (once when expanding, once when shrinking).
Expected Auxiliary Space Complexity: O(1), as we use a fixed-size frequency array of size 256 and a set for unique characters.
🧠 Code (C++)
class Solution {
public:
int findSubString(string& s) {
unordered_set<char> all(s.begin(), s.end());
int n = s.size(), i = 0, j = 0, d = all.size(), c = 0, res = n;
vector<int> freq(256, 0);
while (j < n) {
if (++freq[s[j++]] == 1) c++;
while (c == d) {
res = min(res, j - i);
if (--freq[s[i++]] == 0) c--;
}
}
return res;
}
};🧑💻 Code (Java)
class Solution {
public int findSubString(String s) {
Set<Character> all = new HashSet<>();
for (char c : s.toCharArray()) all.add(c);
int n = s.length(), i = 0, j = 0, d = all.size(), c = 0, res = n;
int[] freq = new int[256];
while (j < n) {
if (++freq[s.charAt(j++)] == 1) c++;
while (c == d) {
res = Math.min(res, j - i);
if (--freq[s.charAt(i++)] == 0) c--;
}
}
return res;
}
}🐍 Code (Python)
class Solution:
def findSubString(self, s):
d = len(set(s))
i = j = c = 0
res = len(s)
freq = [0]*256
while j < len(s):
if freq[ord(s[j])] == 0:
c += 1
freq[ord(s[j])] += 1
j += 1
while c == d:
res = min(res, j - i)
freq[ord(s[i])] -= 1
if freq[ord(s[i])] == 0:
c -= 1
i += 1
return res🧠 Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: 📬 Any Questions?. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated