Created
February 26, 2015 11:29
-
-
Save rohith2506/0041f839c9a92043c2a1 to your computer and use it in GitHub Desktop.
Smallest substring with given string ( Optimized approach )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Very good problem. But difficult to understand. | |
// http://leetcode.com/2010/11/finding-minimum-window-in-s-which.html | |
int search(vector<string> txt, vector<string> pat, string s1, string s2){ | |
for(int i=0; i<s2.length(); i++) pat[(int)(s2[i])]++; | |
txt[256] = {0}; | |
int begin = 0, end = 0; | |
while(end < s1.length()){ | |
if(pat[s1[end]] == 0) continue; | |
txt[s1[end]]++l; | |
if(txt[s1[end]] < pat[s1[end]]) cnt++; | |
if(cnt == s1.length()){ | |
while(pat[s1[begin]] == 0 || txt[s1[begin]] > pat[s1[begin]]){ | |
if(txt[s1[begin]] > pat[s1[begin]]) begin--; | |
begin++; | |
} | |
max_len = max(max_len, (end - begin + 1)); | |
} | |
} | |
return max_len; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment