3297. Count Substrings That Can Be Rearranged to Contain a String I
Description
You are given two strings word1 and word2.
A string x is called valid if x can be rearranged to have word2 as a prefix.
Return the total number of valid substrings of word1.
Example 1:
Input: word1 = "bcca", word2 = "abc"
Output: 1
Explanation:
The only valid substring is "bcca" which can be rearranged to "abcc" having "abc" as a prefix.
Example 2:
Input: word1 = "abcabc", word2 = "abc"
Output: 10
Explanation:
All the substrings except substrings of size 1 and size 2 are valid.
Example 3:
Input: word1 = "abcabc", word2 = "aaabc"
Output: 0
Constraints:
1 <= word1.length <= 1051 <= word2.length <= 104word1andword2consist only of lowercase English letters.
Solutions
Solution 1: Sliding Window
Thinking
A substring can be rearranged to contain \(\textit{word2}\) iff it is a supermultiset of \(\textit{word2}\). Comparing counts on every substring is too slow on a long string. Coverage is monotone: a valid window stays valid when the right end grows, and shrinking the left finds the shortest cover.
Track how many character types are still missing. The right end may decrement that count; while it is \(0\), move the left. Every start to the left of that left end paired with the current right is valid, so add the left index. One slide.
The problem is essentially to find how many substrings in \(\textit{word1}\) contain all the characters in \(\textit{word2}\). We can use a sliding window to handle this.
First, if the length of \(\textit{word1}\) is less than the length of \(\textit{word2}\), then it is impossible for \(\textit{word1}\) to contain all the characters of \(\textit{word2}\), so we directly return \(0\).
Next, we use a hash table or an array of length \(26\) called \(\textit{cnt}\) to count the occurrences of characters in \(\textit{word2}\). Then, we use \(\textit{need}\) to record how many more characters are needed to meet the condition, initialized to the length of \(\textit{cnt}\).
We then use a sliding window \(\textit{win}\) to record the occurrences of characters in the current window. We use \(\textit{ans}\) to record the number of substrings that meet the condition, and \(\textit{l}\) to record the left boundary of the window.
We traverse each character in \(\textit{word1}\). For the current character \(c\), we add it to \(\textit{win}\). If the value of \(\textit{win}[c]\) equals \(\textit{cnt}[c]\), it means the current window already contains one of the characters in \(\textit{word2}\), so we decrement \(\textit{need}\) by one. If \(\textit{need}\) equals \(0\), it means the current window contains all the characters in \(\textit{word2}\). We need to shrink the left boundary of the window until \(\textit{need}\) is greater than \(0\). Specifically, if \(\textit{win}[\textit{word1}[l]]\) equals \(\textit{cnt}[\textit{word1}[l]]\), it means the current window contains one of the characters in \(\textit{word2}\). After shrinking the left boundary, it no longer meets the condition, so we increment \(\textit{need}\) by one and decrement \(\textit{win}[\textit{word1}[l]]\) by one. Then, we increment \(\textit{l}\) by one. At this point, the window is \([l, r]\). For any \(0 \leq l' < l\), \([l', r]\) are substrings that meet the condition, and there are \(l\) such substrings, which we add to the answer.
After traversing all characters in \(\textit{word1}\), we get the answer.
The time complexity is \(O(n + m)\), where \(n\) and \(m\) are the lengths of \(\textit{word1}\) and \(\textit{word2}\), respectively. The space complexity is \(O(|\Sigma|)\), where \(\Sigma\) is the character set. Here, it is the set of lowercase letters, so the space complexity is constant.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |