給定一個字符串 s
和一個由多個單詞組成的列表 words
,請找出所有能夠在字符串 s
中連續拼接所有單詞的子字符串的起始索引。
範例 1:
Input: s = "barfoothefoobarman", words = ["foo", "bar"]
Output: [0,9]
Explanation: 子串 "barfoo" 和 "foobar" 均包含 words 中的所有單詞。
範例 2:
Input: s = "wordgoodgoodgoodbestword", words = ["word", "good", "best", "word"]
Output: []
Explanation: 無法找到包含所有單詞的子串。
範例 3:
Input: s = "barfoofoobarthefoobarman", words = ["bar", "foo", "the"]
Output: [6,9,12]
思路
words
的總長度 total_len
,每個單詞的長度為 word_len
。s
,以每個起始位置檢查長度為 total_len
的子串是否包含所有單詞。words
一致。程式碼實現
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
if not s or not words:
return []
word_len = len(words[0])
total_len = word_len * len(words)
word_count = Counter(words)
n = len(s)
result = []
for i in range(n - total_len + 1):
substring = s[i:i + total_len]
seen = Counter([substring[j:j + word_len] for j in range(0, total_len, word_len)])
if seen == word_count:
result.append(i)
return result
時間與空間複雜度
思路
程式碼實現
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
if not s or not words:
return []
word_len = len(words[0])
total_len = word_len * len(words)
word_count = Counter(words)
n = len(s)
result = []
for i in range(word_len):
left = i
right = i
window_count = Counter()
count = 0
while right + word_len <= n:
word = s[right:right + word_len]
right += word_len
if word in word_count:
window_count[word] += 1
count += 1
while window_count[word] > word_count[word]:
left_word = s[left:left + word_len]
window_count[left_word] -= 1
count -= 1
left += word_len
if count == len(words):
result.append(left)
else:
window_count.clear()
count = 0
left = right
return result
時間與空間複雜度
選擇解法時需考慮輸入規模和時間性能需求。