Task: Find the length of smallest distinct window of given string, which is having every letter in the string at least one time.
Example:
Input:
1
aab
Output:
2
Here is the Code:
Program in C++:
- #include<iostream>
 - #include<bits/stdc++.h>
 - using namespace std;
 - int main()
 - {
 - int t;
 - cin>>t;
 - while(t--)
 - {
 - string s;
 - cin>>s;
 - int vis[128];
 - memset(vis, 0, sizeof(vis));
 - string t,sdw;
 - // Implementation of Logic
 - for(int i=0;i<s.length();i++)
 - {
 - vis[s[i]]++;
 - t=t+s[i];
 - if(vis[s[i]]==1)
 - {
 - sdw=t;
 - }
 - char c;
 - while(1)
 - {
 - c=t[0];
 - vis[c]--;
 - if(vis[c]!=0)
 - {
 - t.erase(t.begin()+0);
 - }
 - else
 - {
 - vis[c]++;
 - break;
 - }
 - }
 - if(t.length()<sdw.length())
 - {
 - sdw=t;
 - }
 - }
 - cout<<sdw.length()<<endl;
 - }
 - return 0;
 - }
 
Here is the Video:
Comments
Post a Comment