You are building a string s of length none character at a time,
prepending each new character to the front of the string. The strings are labeled from 1 to n, where the string with length i is labeled si.
For example, for s = "abaca", s1 == "a", s2 == "ca", s3 == "aca", etc.
The score of si is the length of the longest common prefix between
si and sn (Note that s == sn).
Given the final string s, return _thesum of the score of every _si.
Input: s ="babab"Output: 9Explanation:
For s1 =="b", the longest common prefix is"b" which has a score of 1.For s2 =="ab", there is no common prefix so the score is0.For s3 =="bab", the longest common prefix is"bab" which has a score of 3.For s4 =="abab", there is no common prefix so the score is0.For s5 =="babab", the longest common prefix is"babab" which has a score of 5.The sum of the scores is1+0+3+0+5=9, so we return9.
Input: s ="azbazbzaz"Output: 14Explanation:
For s2 =="az", the longest common prefix is"az" which has a score of 2.For s6 =="azbzaz", the longest common prefix is"azb" which has a score of 3.For s9 =="azbazbzaz", the longest common prefix is"azbazbzaz" which has a score of 9.For all other si, the score is0.The sum of the scores is2+3+9=14, so we return14.
For each si, the score is the length of the longest common prefix between si and s. This is equivalent to computing the Z-array (Z-algorithm) for the string s. The sum of all Z-values plus n (for the full string) gives the answer.
#include<vector>#include<string>usingnamespace std;
classSolution {
public:longlong sumScores(string s) {
int n = s.size();
vector<int> z(n);
longlong res = n;
for (int i =1, l =0, r =0; i < n; ++i) {
if (i <= r) z[i] = min(r - i +1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
if (i + z[i] -1> r) l = i, r = i + z[i] -1;
res += z[i];
}
return res;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
publiclongsumScores(String s) {
int n = s.length();
int[] z =newint[n];
long res = n;
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r) z[i]= Math.min(r - i + 1, z[i - l]);
while (i + z[i]< n && s.charAt(z[i]) == s.charAt(i + z[i])) ++z[i];
if (i + z[i]- 1 > r) { l = i; r = i + z[i]- 1; }
res += z[i];
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funsumScores(s: String): Long {
val n = s.length
val z = IntArray(n)
var res = n.toLong()
var l = 0; var r = 0for (i in1 until n) {
if (i <= r) z[i] = minOf(r - i + 1, z[i - l])
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]
if (i + z[i] - 1 > r) { l = i; r = i + z[i] - 1 }
res += z[i]
}
return res
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defsumScores(self, s: str) -> int:
n = len(s)
z = [0] * n
res = n
l = r =0for i in range(1, n):
if i <= r:
z[i] = min(r - i +1, z[i - l])
while i + z[i] < n and s[z[i]] == s[i + z[i]]:
z[i] +=1if i + z[i] -1> r:
l, r = i, i + z[i] -1 res += z[i]
return res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
impl Solution {
pubfnsum_scores(s: String) -> i64 {
let n = s.len();
let s = s.as_bytes();
letmut z =vec![0; n];
letmut res = n asi64;
let (mut l, mut r) = (0, 0);
for i in1..n {
if i <= r { z[i] = z[i-l].min(r-i+1); }
while i + z[i] < n && s[z[i]] == s[i+z[i]] { z[i] +=1; }
if i + z[i] -1> r { l = i; r = i + z[i] -1; }
res += z[i] asi64;
}
res
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
functionsumScores(s: string):number {
constn=s.length;
constz=new Array(n).fill(0);
letres=n;
letl=0, r=0;
for (leti=1; i<n; ++i) {
if (i<=r) z[i] = Math.min(r-i+1, z[i-l]);
while (i+z[i] <n&&s[z[i]] ===s[i+z[i]]) ++z[i];
if (i+z[i] -1>r) { l=i; r=i+z[i] -1; }
res+=z[i];
}
returnres;
}