The difference between two adjacent characters is at most2. That is, for any two adjacent characters c1 and c2 in s, the absolute difference in their positions in the alphabet is at most2.
Return the number ofcomplete substrings ofword.
A substring is a non-empty contiguous sequence of characters in a string.
Input: word ="igigee", k =2Output: 3Explanation: The complete substrings where each character appears exactly twice and the difference between adjacent characters is at most 2 are: _**igig**_ ee, igig _**ee**_ , _**igigee**_.
Input: word ="aaabbbccc", k =3Output: 6Explanation: The complete substrings where each character appears exactly three times and the difference between adjacent characters is at most 2 are:**_aaa_** bbbccc, aaa _**bbb**_ ccc, aaabbb _**ccc**_ ,**_aaabbb_** ccc, aaa _**bbbccc**_ , _**aaabbbccc**_.
We need to find all substrings where each character appears exactly k times and the difference between adjacent characters is at most 2. Since the substring can have at most 26 unique characters, we can try all possible unique character counts and use a sliding window to check for valid substrings.
classSolution {
public:int countCompleteSubstrings(string word, int k) {
int n = word.size(), ans =0;
for (int u =1; u <=26; ++u) {
int len = u * k;
if (len > n) break;
vector<int> cnt(26, 0);
int uniq =0, over =0;
for (int i =0; i < n; ++i) {
if (i >= len) {
int idx = word[i - len] -'a';
if (--cnt[idx] ==0) uniq--;
if (cnt[idx] == k -1) over--;
}
int idx = word[i] -'a';
if (cnt[idx] ==0) uniq++;
if (cnt[idx] == k -1) over++;
cnt[idx]++;
if (i >= len -1&& uniq == u && over == u) {
bool ok = true;
for (int j = i - len +1; j < i; ++j) {
if (abs(word[j] - word[j +1]) >2) {
ok = false;
break;
}
}
if (ok) ans++;
}
}
}
return ans;
}
};
classSolution {
publicintcountCompleteSubstrings(String word, int k) {
int n = word.length(), ans = 0;
for (int u = 1; u <= 26; ++u) {
int len = u * k;
if (len > n) break;
int[] cnt =newint[26];
int uniq = 0, over = 0;
for (int i = 0; i < n; ++i) {
if (i >= len) {
int idx = word.charAt(i - len) -'a';
if (--cnt[idx]== 0) uniq--;
if (cnt[idx]== k - 1) over--;
}
int idx = word.charAt(i) -'a';
if (cnt[idx]== 0) uniq++;
if (cnt[idx]== k - 1) over++;
cnt[idx]++;
if (i >= len - 1 && uniq == u && over == u) {
boolean ok =true;
for (int j = i - len + 1; j < i; ++j) {
if (Math.abs(word.charAt(j) - word.charAt(j + 1)) > 2) {
ok =false;
break;
}
}
if (ok) ans++;
}
}
}
return ans;
}
}
classSolution {
funcountCompleteSubstrings(word: String, k: Int): Int {
val n = word.length
var ans = 0for (u in1..26) {
val len = u * k
if (len > n) breakval cnt = IntArray(26)
var uniq = 0var over = 0for (i in0 until n) {
if (i >= len) {
val idx = word[i - len] - 'a' cnt[idx]--if (cnt[idx] ==0) uniq--if (cnt[idx] == k - 1) over-- }
val idx = word[i] - 'a'if (cnt[idx] ==0) uniq++if (cnt[idx] == k - 1) over++ cnt[idx]++if (i >= len - 1&& uniq == u && over == u) {
var ok = truefor (j in i - len + 1 until i) {
if (kotlin.math.abs(word[j] - word[j + 1]) > 2) {
ok = falsebreak }
}
if (ok) ans++ }
}
}
return ans
}
}
classSolution:
defcountCompleteSubstrings(self, word: str, k: int) -> int:
n, ans = len(word), 0for u in range(1, 27):
l = u * k
if l > n: break cnt = [0] *26 uniq = over =0for i in range(n):
if i >= l:
idx = ord(word[i - l]) - ord('a')
cnt[idx] -=1if cnt[idx] ==0: uniq -=1if cnt[idx] == k -1: over -=1 idx = ord(word[i]) - ord('a')
if cnt[idx] ==0: uniq +=1if cnt[idx] == k -1: over +=1 cnt[idx] +=1if i >= l -1and uniq == u and over == u:
ok =Truefor j in range(i - l +1, i):
if abs(ord(word[j]) - ord(word[j +1])) >2:
ok =Falsebreakif ok: ans +=1return ans
impl Solution {
pubfncount_complete_substrings(word: String, k: i32) -> i32 {
let n = word.len();
let w = word.as_bytes();
letmut ans =0;
for u in1..=26 {
let l = u * k asusize;
if l > n { break; }
letmut cnt = [0; 26];
let (mut uniq, mut over) = (0, 0);
for i in0..n {
if i >= l {
let idx = (w[i - l] -b'a') asusize;
cnt[idx] -=1;
if cnt[idx] ==0 { uniq -=1; }
if cnt[idx] == k -1 { over -=1; }
}
let idx = (w[i] -b'a') asusize;
if cnt[idx] ==0 { uniq +=1; }
if cnt[idx] == k -1 { over +=1; }
cnt[idx] +=1;
if i +1>= l && uniq == u && over == u {
letmut ok =true;
for j in (i +1- l)..i {
if (w[j] asi32- w[j +1] asi32).abs() >2 {
ok =false;
break;
}
}
if ok { ans +=1; }
}
}
}
ans
}
}