Topic:
Consider the string s to is the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s would look like this: ". . Zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd .....
Now we have another string p. Your job is the to find out how many unique non-empty substrings of P was present in S. In particular, your input was the string p and you need to output the number of different non-empty substrings of P in the String S.
Note:p consists of only lowercase 中文版 letters and the size of p might be over 10000.
Example 1:
Input: "A"
output:1
explanation:only the substring "a" of string "a" is in the string s.
Example 2:
Input: "CAC"
output:2
Explanation:there is-substrings "a", "C" of string "CAC" in the string s.
Example 3:
Input: "Zab"
output:6
Explanation:there is six substrings "Z", "a", "B", "Za", "AB", "Zab" of string "Zab" in t He string S.
Subscribe to see which companies asked this question code:
Class Solution {public
:
int findsubstringinwraproundstring (string p) {
int size = P.size (), totalsum=0;
if (size = = 0) return 0;
Vector<int> count (26,0);
for (int i = 0; i < size-1; i++) {
int k = i,cou=1;
while (k<size-1&& (p[k+1]-p[k] = = 1 | | p[k]-p[k+1] = = 25)) {//Two kinds of special cases
k++;
cou++;
}
if (Cou>count[p[i]-' a ']) count[p[i]-' a '] = cou;
}
Process the last element
if (count[p[size-1]-' a '] = = 0) count[p[size-1]-' a '] = 1;
for (int i = 0; i < i++) Totalsum + = Count[i];
return totalsum;
}
;
Operation Result:
Submission result:accepted more Details Next challenges: (H) Distinct subsequences (m) sentence screen Fitting (m) Can I Win time: 1553ms
Analysis:
The target string z and a are connected, i.e. "... zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd ..." When the front of a is Z, it can still be seen as a continuous substring. In the implementation of the algorithm, the biggest problem is the appearance of similar "ABCABCD",
The substring in front of "ABC" will appear: A,B,C,AB,BC,ABC;
The following "ABCD" will appear in the substring is: A,B,C,D,AB,BC,CD,ABC,BCD,ABCD, in the application of common methods to accumulate, there will be repeated statistics, when the first reaction is to establish a hashmap, However, the complexity of the space and the need to add each occurrence of the sub-string into the hashmap, the implementation of a greater difficulty;
However, in a different way of thinking, in the string type P, the letter that appears is 26, if we find a substring beginning with each letter, and then add them together, we can get all the substrings, such as:
Example:xyzabcefg
Longest (x) = 6, ==> we get 6 sub strings started from X, x XY xyz Xyza Xyzab XYZABC
Longest (Y) = 5 ==> y yz Yza Yzab yzabc
Longest (c) = longest (g) = 1 ==> C, g
So the problem can become simple. This is how to obtain longest (a). For the above "ABCABCD" we can find two of the length of all substrings starting with a is 6 and 10, then we will take a larger value, because as long as the continuous occurrence of a as the beginning of the string form must be "abcd...xyzabc ..." So long strings must contain all substrings of a short length, just as the ABCD substring contains all the substrings of ABC ...
Here's another, more concise approach:
Class solution{public
:
int findsubstringinwraproundstring (string p) {
vector<int> letters (0);
int res = 0, len = 0;
for (int i = 0; i < p.size (); i++) {
int cur = p[i]-' a ';
if (i > 0 && p[i-1]! = (cur + 26-1)% + ' a ') len = 0;
if (++len > Letters[cur]) {
res + = Len-letters[cur];
Letters[cur] = len;
}
}
return res;
}
};