comments | difficulty | edit_url | tags | ||
---|---|---|---|---|---|
true |
Medium |
|
We define the string base
to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz"
, so base
will look like this:
"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd...."
.
Given a string s
, return the number of unique non-empty substrings of s
are present in base
.
Example 1:
Input: s = "a" Output: 1 Explanation: Only the substring "a" of s is in base.
Example 2:
Input: s = "cac" Output: 2 Explanation: There are two substrings ("a", "c") of s in base.
Example 3:
Input: s = "zab" Output: 6 Explanation: There are six substrings ("z", "a", "b", "za", "ab", and "zab") of s in base.
Constraints:
1 <= s.length <= 105
s
consists of lowercase English letters.
We can define an array
We define a variable
Finally, we return the sum of all elements in
The time complexity is
class Solution:
def findSubstringInWraproundString(self, s: str) -> int:
f = defaultdict(int)
k = 0
for i, c in enumerate(s):
if i and (ord(c) - ord(s[i - 1])) % 26 == 1:
k += 1
else:
k = 1
f[c] = max(f[c], k)
return sum(f.values())
class Solution {
public int findSubstringInWraproundString(String s) {
int[] f = new int[26];
int n = s.length();
for (int i = 0, k = 0; i < n; ++i) {
if (i > 0 && (s.charAt(i) - s.charAt(i - 1) + 26) % 26 == 1) {
++k;
} else {
k = 1;
}
f[s.charAt(i) - 'a'] = Math.max(f[s.charAt(i) - 'a'], k);
}
return Arrays.stream(f).sum();
}
}
class Solution {
public:
int findSubstringInWraproundString(string s) {
int f[26]{};
int n = s.length();
for (int i = 0, k = 0; i < n; ++i) {
if (i && (s[i] - s[i - 1] + 26) % 26 == 1) {
++k;
} else {
k = 1;
}
f[s[i] - 'a'] = max(f[s[i] - 'a'], k);
}
return accumulate(begin(f), end(f), 0);
}
};
func findSubstringInWraproundString(s string) (ans int) {
f := [26]int{}
k := 0
for i := range s {
if i > 0 && (s[i]-s[i-1]+26)%26 == 1 {
k++
} else {
k = 1
}
f[s[i]-'a'] = max(f[s[i]-'a'], k)
}
for _, x := range f {
ans += x
}
return
}
function findSubstringInWraproundString(s: string): number {
const idx = (c: string): number => c.charCodeAt(0) - 97;
const f: number[] = Array(26).fill(0);
const n = s.length;
for (let i = 0, k = 0; i < n; ++i) {
const j = idx(s[i]);
if (i && (j - idx(s[i - 1]) + 26) % 26 === 1) {
++k;
} else {
k = 1;
}
f[j] = Math.max(f[j], k);
}
return f.reduce((acc, cur) => acc + cur, 0);
}
impl Solution {
pub fn find_substring_in_wrapround_string(s: String) -> i32 {
let idx = |c: u8| -> usize { (c - b'a') as usize };
let mut f = vec![0; 26];
let n = s.len();
let s = s.as_bytes();
let mut k = 0;
for i in 0..n {
let j = idx(s[i]);
if i > 0 && ((j as i32) - (idx(s[i - 1]) as i32) + 26) % 26 == 1 {
k += 1;
} else {
k = 1;
}
f[j] = f[j].max(k);
}
f.iter().sum()
}
}