Find Substring with Concatenation of All Words

Advanced

Welcome to the colorful world of strings and words! In this challenge, we're on a detective mission. We have a long string that contains a secret sentence created by mixing and matching various words. Our job is to find the starting index of the part of the string where these words, in any order, make up a complete section.

Let's get cracking! We will write a function named find_substring_with_concatenation_of_all_words. This function takes two parameters:

  1. s: A long string where we need to search for words.
  2. words: A list of words that, when concatenated together in any order, can form a substring in s.

Our goal is to return the starting index of the first occurrence of this special substring in s. The substring should be a concatenation of each word from words exactly once and without any other characters in between them.

Examples

# Example 1 s = "barfoothefoobarman" words = ["foo", "bar"] # "foobar" is at index 0; "barfoo" is at index 3 # So, the output should be 0 because it starts first. print(find_substring_with_concatenation_of_all_words(s, words)) # Output: 0 # Example 2 s = "wordgoodgoodgoodbestword" words = ["word", "good", "best", "good"] # "goodgoodbestword" starts at index 8 print(find_substring_with_concatenation_of_all_words(s, words)) # Output: 8 # Example 3 s = "lingmindraboofooowingdingbarrwingmonkeypoundcake" words = ["fooo", "barr", "wing", "ding", "wing"] # "fooowingdingbarrwing" starts at index 13 and uses every word exactly once print(find_substring_with_concatenation_of_all_words(s, words)) # Output: 13

Important Notes

  • If no such starting index is found, return -1.
  • All input strings only contain lowercase English letters.
  • It's guaranteed that all words are of the same length.

Get your detective hat and magnifying glass ready, let's find some wordy treasures!

Geek
Inputs

Arg 1:

"barfoothefoobarman"

Arg 2:

["foo","bar"]
Expected Output
0
Actual Output
Find Substring with Concatenation of All Words [advanced] | Coding for Kids