Skip to content

Commit 543eace

Browse files
committed
smallest window
1 parent d884c3a commit 543eace

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

DSA Crack Sheet/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
- [Second most repeated string in a sequence](https://practice.geeksforgeeks.org/problems/second-most-repeated-string-in-a-sequence0534/1# "view question") - [Cpp Solution](./solutions/Second%20most%20repeated%20string%20in%20a%20sequence.cpp)
8585
- [Minimum Swaps for Bracket Balancing](https://practice.geeksforgeeks.org/problems/minimum-swaps-for-bracket-balancing/0# "view question") - [Cpp Solution](./solutions/Minimum%20Swaps%20for%20Bracket%20Balancing.cpp)
8686
- [Longest Common Subsequence](https://practice.geeksforgeeks.org/problems/longest-common-subsequence-1587115620/1 "view question") - [Cpp Solution](./solutions/Longest%20Common%20Subsequence.cpp)
87+
- [Program to generate all possible valid IP addresses from given string](https://www.geeksforgeeks.org/program-generate-possible-valid-ip-addresses-given-string/ "view topic")
88+
- [Smallest distinct window](https://practice.geeksforgeeks.org/problems/smallest-distant-window3132/1 "view question") - [Cpp Solution](./solutions/Smallest%20distinct%20window.cpp)
8789
- []( "view question") - [Cpp Solution](./solutions/.cpp)
8890

8991
### Searching & Sorting
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Smallest distinct window
3+
========================
4+
5+
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
6+
For eg. A = “aabcbcdbca”, then the result would be 4 as of the smallest window will be “dbca”.
7+
8+
Example 1:
9+
Input : "AABBBCBBAC"
10+
Output : 3
11+
Explanation : Sub-string -> "BAC"
12+
13+
Example 2:
14+
Input : "aaab"
15+
Output : 2
16+
Explanation : Sub-string -> "ab"
17+
18+
Example 3:
19+
Input : "GEEKSGEEKSFOR"
20+
Output : 8
21+
Explanation : Sub-string -> "GEEKSFOR"
22+
23+
Your Task:
24+
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as inputs and returns the length of the smallest such string.
25+
26+
Expected Time Complexity: O(256.N)
27+
Expected Auxiliary Space: O(256)
28+
29+
Constraints:
30+
1 ≤ |S| ≤ 105
31+
String may contain both type of English Alphabets.
32+
*/
33+
34+
string findSubString(string str)
35+
{
36+
int n = str.size();
37+
int dist_count = 0;
38+
bool visited[256] = {false};
39+
40+
for (int i = 0; i < n; i++)
41+
{
42+
if (visited[str[i]] == false)
43+
{
44+
visited[str[i]] = true;
45+
dist_count++;
46+
}
47+
}
48+
49+
int start = 0, start_index = -1, min_len = INT_MAX;
50+
51+
int count = 0;
52+
int curr_count[256] = {0};
53+
54+
for (int i = 0; i < n; ++i)
55+
{
56+
curr_count[str[i]]++;
57+
if (curr_count[str[i]] == 1)
58+
count++;
59+
if (count == dist_count)
60+
{
61+
while (curr_count[str[start]] > 1)
62+
{
63+
if (curr_count[str[start]] > 1)
64+
curr_count[str[start]]--;
65+
start++;
66+
}
67+
68+
int len_window = i - start + 1;
69+
if (min_len > len_window)
70+
{
71+
min_len = len_window;
72+
start_index = start;
73+
}
74+
}
75+
}
76+
77+
return str.substr(start_index, min_len);
78+
}

0 commit comments

Comments
 (0)