Substring with Concatenation of All Words - Problem

You're given a string s and an array of strings words, where all strings in words have the same length. Your task is to find all starting indices in s where a concatenated substring begins.

A concatenated substring is formed by joining all strings from words in any order, using each word exactly once. For example, if words = ["ab", "cd", "ef"], then valid concatenated strings include:

  • "abcdef", "abefcd", "cdabef"
  • "cdefab", "efabcd", "efcdab"

However, "acdbef" would not be valid because it doesn't represent any permutation of the original words.

Goal: Return an array of all starting indices where these concatenated substrings appear in s.

Input & Output

example_1.py โ€” Basic case
$ Input: s = "barfoothe", words = ["foo","bar"]
โ€บ Output: [0, 3]
๐Ÿ’ก Note: At index 0: "barfoo" can be split into "bar" + "foo". At index 3: "foobar" can be split into "foo" + "bar". Both use all words exactly once.
example_2.py โ€” No valid concatenation
$ Input: s = "wordgoodgoodgoodword", words = ["word","good","best","good"]
โ€บ Output: []
๐Ÿ’ก Note: No substring contains all required words. We need "word", "good", "best", and "good" (note: "good" appears twice in words array), but "best" never appears in string s.
example_3.py โ€” Multiple valid positions
$ Input: s = "barfoobar", words = ["foo","bar"]
โ€บ Output: [0, 3]
๐Ÿ’ก Note: At index 0: "barfoo" = "bar" + "foo". At index 3: "foobar" = "foo" + "bar". Index 6 would be "bar" alone, which doesn't use all words.

Visualization

Tap to expand
b a r f o o t h e b a r m a nbarfooPerfect Fit!thebarNo match - 'the' not in word listGreen/Blue = Target words, Gray = Invalid combinations
Understanding the Visualization
1
Set up the puzzle
We have a long string (puzzle strip) and word pieces that must fit exactly
2
Try different starting positions
For each possible alignment, slide a window across the strip
3
Check piece fit
At each position, verify that the pieces match our target collection exactly
4
Record solutions
When pieces align perfectly, mark that position as a solution
Key Takeaway
๐ŸŽฏ Key Insight: By using sliding windows with different starting offsets and maintaining word frequency counts, we can efficiently find all valid concatenation positions without redundant work.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * w)

We process each character at most a constant number of times across all sliding windows

n
2n
โœ“ Linear Growth
Space Complexity
O(m * w)

Space for hash maps storing word frequencies and current window state

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 104
  • 1 โ‰ค words.length โ‰ค 5000
  • 1 โ‰ค words[i].length โ‰ค 30
  • All strings of words are the same length
  • s and words[i] consist of lowercase English letters only
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28 Apple 22
58.6K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen