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.
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
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.
Название |
---|
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