13 · Sliding Window

#30 Substring with Concatenation of All Words

Hardsolve on LeetCode ↗

frequently reported atSamsungAmazonGoogleMicrosoft

the problem

In plain words

Every string in words has the same length w. A "concatenation" is all the words joined end to end in any order, each used exactly as many times as it appears in the list. Return every index of s where such a concatenation begins, in any order.

in s = "barfoothefoobarman", words = ["foo","bar"]

out [0,9]

"barfoo" starts at 0 and "foobar" starts at 9.

in s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]

out []

"word" is needed twice, and no 16-letter stretch has two of them plus "good" and "best".

in s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]

out [6,9,12]

  • 1 ≤ s.length ≤ 10⁴
  • 1 ≤ words.length ≤ 5000
  • 1 ≤ words[i].length ≤ 30, all the same
  • Lowercase English letters

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #438
      Find All Anagrams in a String
      Mediumin Sliding Window
    2. #76
      Minimum Window Substring
      Hardin Sliding Window
    3. #49
      Group Anagrams
      Mediumin Arrays & Strings