pyassistant

Concatenation of Given Words (variable lengths)

Given a string s and a list of words, find all starting indices in s where a substring is formed by concatenating each word from words exactly once and without any intervening characters. Words may have different lengths and may repeat in the list; each occurrence in the list must be used exactly once. Return the starting indices in ascending order. If no such substring exists, return an empty list. Example: s = "foobarbazfoo", words = ["bar", "foo"] — valid substrings are "foobar" starting at 0 and "barfoo" starting at 3, so result is [0, 3].

Example:

Input:
("foobarbazfoo", ["bar", "foo"])
Output:
[0, 3]

Make sure you return your solution, don't print!

AI

Bot

Trying to solve my challenge? Ask if you must, or press the purple button so I can analyze your code.