Given a string s with size n, how do I find the minimun range (p,q) such that all the characters in s occur in that range. Looking for an O(n) solution.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 151 |
Given a string s with size n, how do I find the minimun range (p,q) such that all the characters in s occur in that range. Looking for an O(n) solution.
Name |
---|
First hash the characters of string then use two pointer approach.
first you find out the number of different characters in the given string using hashing.. . Now you have to find out the smallest window in the given string such that it contains all distinct characters.
val=no.of distinct characters.
now to do this you can use two pointer technique.. how?? take st=0 (left point of window) and take i=0 (right point of window initially) . keep adding characters till count of distinct is less than val.. (my variable is cur) as soon as you reach cur = val then you try to remove characters from back to reduce window size making sure that your cur is equal to value.. so u shift st for that.. as you have shifted st to the point where you could now add a character each time and remove characters from back each time and check current window size for minimum.. you will get it when you work out on paper.. if you still have some doubts let me know.
I actually came up with two solutions one of them is really similar to this. However, I am not sure if your solution can handle the case where the character at st does not occur again in a short range. That is, you always count the number of distinct characters but left endpoint can decrease that when shifted, how will your algorithm respond to that?
For each window keep the frequency of each characters present in it and keep the count of no of characters having frequency greater than 0. If this value comes out to be no of distinct characters in the string, then consider that range for answer.
I came up with two solutions for this question lately.
1--> *I determine distinct elements by hashing in O(n) time. *I take the indices of all characters from the original string using different stacks for all distinct characters. *I start eleminating them from start as they are sorted(by nature as we start from the beginning of the string) and look for the minimum range in every step. *I output the minimum range.
2--> *I determine distinct elements by hashing in O(n) time. *Create an array a[] that has a size of the number of distinct characters. *I start from the beginning of the string s and increase the number of occurrence of character s[i] in a. That is; a[s[i]]++; *When I first reach to a state that all of the distinct elements have occurred at least once, I look for the left endpoint and if the character there has a number of occurrence(a[s[left_endpoint]]) more than 1 than I can reduce that. *I repeat while(a[s[left_endpoint]]>1) *Then I reduce the left endpoint to see if I can have different substrings on the right that has the expected property.
It also takes O(n) time.
If you see a problem please inform me, thank you!
http://codeforces.net/problemset/problem/701/C