Input: word1 ="bcca", word2 ="abc"Output: 1Explanation:
The only valid substring is`"bcca"` which can be rearranged to `"abcc"`having `"abc"` as a prefix.
To check if a substring of word1 can be rearranged to have word2 as a prefix, the substring must have at least as many of each character as in word2. We can use a sliding window of length at least len(word2) and count frequencies to check this efficiently.
Count the frequency of each character in word2 (let’s call this need).
For each possible substring of word1 of length at least len(word2), use a sliding window to maintain the frequency of characters in the current window.
For each window, check if the window’s frequency covers all characters in need (i.e., for every character, window count >= need count).
classSolution {
public:longlong countValidSubstrings(string word1, string word2) {
int n = word1.size(), m = word2.size();
vector<int> need(26, 0);
for (char c : word2) need[c -'a']++;
longlong ans =0;
for (int l =0; l <= n - m; ++l) {
vector<int> cnt(26, 0);
for (int r = l; r < n; ++r) {
cnt[word1[r] -'a']++;
if (r - l +1>= m) {
bool ok = true;
for (int i =0; i <26; ++i) {
if (cnt[i] < need[i]) { ok = false; break; }
}
if (ok) ans++;
}
}
}
return ans;
}
};
classSolution {
publiclongcountValidSubstrings(String word1, String word2) {
int n = word1.length(), m = word2.length();
int[] need =newint[26];
for (char c : word2.toCharArray()) need[c -'a']++;
long ans = 0;
for (int l = 0; l <= n - m; ++l) {
int[] cnt =newint[26];
for (int r = l; r < n; ++r) {
cnt[word1.charAt(r) -'a']++;
if (r - l + 1 >= m) {
boolean ok =true;
for (int i = 0; i < 26; ++i) {
if (cnt[i]< need[i]) { ok =false; break; }
}
if (ok) ans++;
}
}
}
return ans;
}
}
classSolution {
funcountValidSubstrings(word1: String, word2: String): Long {
val n = word1.length
val m = word2.length
val need = IntArray(26)
for (c in word2) need[c - 'a']++var ans = 0Lfor (l in0..n-m) {
val cnt = IntArray(26)
for (r in l until n) {
cnt[word1[r] - 'a']++if (r - l + 1>= m) {
var ok = truefor (i in0..25) {
if (cnt[i] < need[i]) { ok = false; break }
}
if (ok) ans++ }
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defcountValidSubstrings(self, word1: str, word2: str) -> int:
n, m = len(word1), len(word2)
need = [0] *26for c in word2:
need[ord(c) - ord('a')] +=1 ans =0for l in range(n - m +1):
cnt = [0] *26for r in range(l, n):
cnt[ord(word1[r]) - ord('a')] +=1if r - l +1>= m:
if all(cnt[i] >= need[i] for i in range(26)):
ans +=1return ans
impl Solution {
pubfncount_valid_substrings(word1: String, word2: String) -> i64 {
let n = word1.len();
let m = word2.len();
let word1 = word1.as_bytes();
let word2 = word2.as_bytes();
letmut need =vec![0; 26];
for&c in word2.iter() {
need[(c -b'a') asusize] +=1;
}
letmut ans =0i64;
for l in0..=n-m {
letmut cnt =vec![0; 26];
for r in l..n {
cnt[(word1[r] -b'a') asusize] +=1;
if r - l +1>= m {
if (0..26).all(|i| cnt[i] >= need[i]) {
ans +=1;
}
}
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
countValidSubstrings(word1: string, word2: string):number {
constn=word1.length, m=word2.length;
constneed= Array(26).fill(0);
for (constcofword2) need[c.charCodeAt(0) -97]++;
letans=0;
for (letl=0; l<=n-m; ++l) {
constcnt= Array(26).fill(0);
for (letr=l; r<n; ++r) {
cnt[word1.charCodeAt(r) -97]++;
if (r-l+1>=m) {
if (need.every((v, i) =>cnt[i] >=v)) ans++;
}
}
}
returnans;
}
}