30. 串聯(lián)所有單詞的子串(C++)【超時】
給定一個字符串?s
?和一個字符串?dāng)?shù)組?words
。?words
?中所有字符串?長度相同。
?s
?中的?串聯(lián)子串?是指一個包含??words
?中所有字符串以任意順序排列連接起來的子串。
例如,如果?
words = ["ab","cd","ef"]
, 那么?"abcdef"
,?"abefcd"
,"cdabef"
,?"cdefab"
,"efabcd"
, 和?"efcdab"
?都是串聯(lián)子串。?"acdbef"
?不是串聯(lián)子串,因為他不是任何?words
?排列的連接。
返回所有串聯(lián)字串在?s
?中的開始索引。你可以以?任意順序?返回答案。
?
示例 1:
輸入:s = "barfoothefoobarman", words = ["foo","bar"]輸出:[0,9]
解釋:因為 words.length == 2 同時 words[i].length == 3,連接的子字符串的長度必須為 6。
子串 "barfoo" 開始位置是 0。它是 words 中以 ["bar","foo"] 順序排列的連接。
子串 "foobar" 開始位置是 9。它是 words 中以 ["foo","bar"] 順序排列的連接。
輸出順序無關(guān)緊要。返回 [9,0] 也是可以的。
示例 2:
輸入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]輸出:[]
解釋:因為 words.length == 4 并且 words[i].length == 4,所以串聯(lián)子串的長度必須為 16。
s 中沒有子串長度為 16 并且等于 words 的任何順序排列的連接。
所以我們返回一個空數(shù)組。
示例 3:
輸入:s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]輸出:[6,9,12]解釋:因為 words.length == 3 并且 words[i].length == 3,所以串聯(lián)子串的長度必須為 9。 子串 "foobarthe" 開始位置是 6。它是 words 中以 ["foo","bar","the"] 順序排列的連接。 子串 "barthefoo" 開始位置是 9。它是 words 中以 ["bar","the","foo"] 順序排列的連接。 子串 "thefoobar" 開始位置是 12。它是 words 中以 ["the","foo","bar"] 順序排列的連接。
?
提示:
1 <= s.length <= 104
1 <= words.length <= 5000
1 <= words[i].length <= 30
words[i]
?和?s
?由小寫英文字母組成
刷題代碼(通過175/178)
本地調(diào)試代碼