Watch 6 video solutions for Count Substrings That Can Be Rearranged to Contain a String I, a medium level problem involving Hash Table, String, Sliding Window. This walkthrough by codingMohan has 1,812 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 104word1 and word2 consist only of lowercase English letters.Problem Overview: You are given a string and another target string. The task is to count how many substrings can be rearranged so that the substring contains all characters of the target string. Since rearrangement is allowed, only the character frequencies matter, not their order.
Approach 1: Sliding Window with Character Count (O(n) time, O(1) space)
This approach uses the classic sliding window technique combined with a character frequency map. First compute the required frequency of each character from the target string. Expand the right pointer across the main string while updating counts in the window. When the window has at least the required frequency for every character, the substring can be rearranged to contain the target.
Once a valid window is found, every extension of that window to the right is also valid. If the current right index is r, then all substrings ending from r to n-1 work, so you add n - r to the answer. Then shrink the window from the left to find the next minimal valid window. Frequency checks are constant time using an array or hash table. The full scan runs in O(n) time with O(1) space since the alphabet size is fixed.
Approach 2: Prefix Sum with Character Frequency (O(26 · n log n) time, O(26 · n) space)
This method precomputes prefix frequency arrays for each character in the string using a string preprocessing technique. With prefix sums, you can quickly compute the frequency of any character inside a substring [l, r] in constant time. For each starting index l, search for the smallest r where the substring contains all required frequencies of the target string.
Once such an r is found, every larger ending index also forms a valid substring. Add n - r to the result and move to the next starting index. The frequency check uses prefix differences, and finding the minimal valid r can be done with binary search or incremental scanning. This approach is easier to reason about when you already have prefix frequency infrastructure but uses more memory.
Recommended for interviews: Sliding window with character counts is the expected solution. It runs in O(n) time, uses constant space, and demonstrates strong understanding of frequency tracking and window contraction. The prefix sum method is conceptually clean but heavier in both memory and implementation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sliding Window with Character Count | O(n) | O(1) | Best general solution when scanning substrings with frequency constraints |
| Prefix Sum with Character Frequency | O(26 · n log n) | O(26 · n) | Useful when prefix frequency arrays are already available or substring queries are frequent |