3297. 统计重新排列后包含另一个字符串的子字符串数目
提示
给你两个字符串 word1 和 word2 。
如果一个字符串 x 重新排列后,word2 是重排字符串的 前缀
字符串的前缀是从字符串的开头开始并延伸到其中任意点的子串。
x 是 合法的 。
请你返回 word1 中 合法
示例 1:
输入:word1 = "bcca", word2 = "abc"
输出:1
解释:
唯一合法的子字符串是 "bcca" ,可以重新排列得到 "abcc" ,"abc" 是它的前缀。
示例 2:
输入:word1 = "abcabc", word2 = "abc"
输出:10
解释:
除了长度为 1 和 2 的所有子字符串都是合法的。
示例 3:
输入:word1 = "abcabc", word2 = "aaabc"
输出:0
解释:
1 <= word1.length <= 1051 <= word2.length <= 104word1和word2都只包含小写英文字母。
/** * @param {string} word1 * @param {string} word2 * @return {number} */ var validSubstringCount = function (word1, word2) { const diff = new Array(26).fill(0); const aNum = 'a'.charCodeAt(0); for (const c of word2) { diff[c.charCodeAt(0) - aNum]--; } let res = 0; let cnt = diff.filter(c => c < 0).length; const update = (c, add) => { diff[c] += add; if (add === 1 && diff[c] === 0) { cnt--; } else if (add === -1 && diff[c] === -1) { cnt++; } } let l = 0, r = 0; let n = word1.length; while (l < n) { while (r < n && cnt > 0) { update(word1.charCodeAt(r) - aNum, 1); r++; } if (cnt === 0) { res += n - r + 1; } update(word1.charCodeAt(l) - aNum, -1); l++; } return res; };

浙公网安备 33010602011771号