Given a string s and a list of words words, where each word is the same length, find all starting indices of substrings in s that is a concatenation of every word in words exactly once.For example, given s = "dogcatcatcodecatdog" and words = ["cat", "dog"], return [0, 13], since "dogcat" starts at index 0 and "catdog" starts at index 13.Given s = "barfoobazbitbyte" and words = ["dog", "cat"], return -1 since there are no substrings composed of "dog" and "cat" in s.Tescase 1:Input:dogcatcatcodecatdog // string2 // number of wordscat dog // words separated by spaceOutput:[0, 13]Testcase 2:Input:barfoobazbitbyte // string2 // number of wordscat dog // words separated by spaceOutput:-1
Question
Given a string s and a list of words words, where each word is the same length, find all starting indices of substrings in s that is a concatenation of every word in words exactly once.For example, given s = "dogcatcatcodecatdog" and words = ["cat", "dog"], return [0, 13], since "dogcat" starts at index 0 and "catdog" starts at index 13.Given s = "barfoobazbitbyte" and words = ["dog", "cat"], return -1 since there are no substrings composed of "dog" and "cat" in s.Tescase 1:Input:dogcatcatcodecatdog // string2 // number of wordscat dog // words separated by spaceOutput:[0, 13]Testcase 2:Input:barfoobazbitbyte // string2 // number of wordscat dog // words separated by spaceOutput:-1
Solution
The problem is asking to find all the starting indices of substrings in a given string that is a concatenation of every word in a given list of words exactly once. Each word in the list has the same length. If there are no such substrings, return -1.
Here is a step-by-step solution in Python:
- First, define a function that takes a string and a list of words as parameters.
def findSubstring(s, words):
- Check if the list of words is empty. If it is, return an empty list.
if not words:
return []
- Calculate the length of a word and the total length of all words.
wordLength = len(words[0])
totalLength = len(words) * wordLength
- Initialize an empty list to store the starting indices of the substrings.
indices = []
- Iterate over the string. For each character, check if the substring starting at that character and having the total length of all words is a concatenation of the words. If it is, add the index to the list.
for i in range(len(s) - totalLength + 1):
if sorted(s[i:i+totalLength:wordLength]) == sorted(words):
indices.append(i)
- If the list of indices is empty, return -1. Otherwise, return the list.
return indices if indices else -1
- Call the function with the given string and list of words.
print(findSubstring("dogcatcatcodecatdog", ["cat", "dog"])) # Output: [0, 13]
print(findSubstring("barfoobazbitbyte", ["dog", "cat"])) # Output: -1
This solution works by checking every possible substring of the given string that has the same length as the total length of all words. It sorts the characters in the substring and the words, and compares them. If they are equal, it means the substring is a concatenation of the words.
Similar Questions
Iterate through the words list and concatenate each word to result
Given a list of words, find all pairs of unique indices such that the concatenation of the two words is a palindrome.For example,given the list ["code", "edoc", "da", "d"], return [(0, 1), (1, 0), (2, 3)].Sample Test CasesTest Case 1:Expected Output:Enter·the·words·of·the·list·separated·by·space:·code edoc da dPairs·of·unique·indices·that·make·palindrome·are:·[(0,·1),·(1,·0),·(2,·3)]
Define a function first_word(arg1),with a string argument arg1. In the function:a. use split() function to separate arg1 by "," into a list of wordsb. Return res that is the last element of the list of words
You are given an array of strings arr. A string s is formed by the concatenation of a subsequence of arr that has unique characters.Return the maximum possible length of s.A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. Example 1:Input: arr = ["un","iq","ue"]Output: 4Explanation: All the valid concatenations are:- ""- "un"- "iq"- "ue"- "uniq" ("un" + "iq")- "ique" ("iq" + "ue")Maximum length is 4.
Given a string A and two integer B and C.Find and return the substring of A starting from index B and ending with index C.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.