3298. Count Substrings That Can Be Rearranged to Contain a String II
3298. Count Substrings That Can Be Rearranged to Contain a String II
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.
Note that the memory limits in this problem are smaller than usual, so you must implement a solution with a linear runtime complexity.
Example 1:
1 | Input: word1 = "bcca", word2 = "abc" |
Explanation:
The only valid substring is "bcca" which can be rearranged to "abcc" having "abc" as a prefix.
Example 2:
1 | Input: word1 = "abcabc", word2 = "abc" |
Explanation:
All the substrings except substrings of size 1 and size 2 are valid.
Example 3:
1 | Input: word1 = "abcabc", word2 = "aaabc" |
Constraints:
1 <= word1.length <= 10^61 <= word2.length <= 10^4word1andword2consist only of lowercase English letters.
Hints/Notes
- 2024/09/14
- sliding window
- t3 is the same question with less number
- 0x3Fâs solution(checked)
- Weekly Contest 416 T4
Solution
Language: C++
1 | class Solution { |