Skip to content
Decoder

Substring with Concatenation of All Words

Hard

Problem

You are given a string s and a list words whose strings all have the same length. A chain is any string you can make by gluing together every word in words exactly once, in any order (a word listed twice must be used twice). Return the starting index of every substring of `s` that is a chain, in any order.

Example 1

Input
s = "dogcatcatdog", words = ["cat", "dog"]
Output
[0, 6]
Why
"dogcat", "catdog"

Example 2

Input
s = "aaaa", words = ["aa", "aa"]
Output
[0]

Example 3

Input
s = "xyzab", words = ["ab", "cd"]
Output
[]

Constraints

  • 1 <= s.length <= 10^4
  • 1 <= words.length <= 5000
  • 1 <= words[i].length <= 30
  • s and every word contain only lowercase English letters.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.