LeetCode Everyday:坚持价值投资,做时间的朋友!!!
题目:
给定一个字符串s
和一些长度相同的单词 words。找出s
中恰好可以由words
中所有单词串联形成的子串的起始位置。注意子串要与words
中的单词完全匹配,中间不能有其他字符,但不需要考虑words
中单词串联的顺序。
示例:
- 示例 1:
输入:s = "barfoothefoobarman", words = ["foo","bar"] 输出:[0,9] 解释:从索引 0 和 9 开始的子串分别是 "barfoo" 和 "foobar" 。输出的顺序不重要, [9,0] 也是有效答案。
- 示例 2:
输入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"] 输出:[]
代码
方法一: 因为单词长度固定的,我们可以计算出截取字符串的单词个数是否和 words 里相等,所以我们可以借用哈希表。
一个是哈希表是 words,一个哈希表是截取的字符串,比较两个哈希是否相等!因为遍历和比较都是线性的,所以时间复杂度:O(n^2)
题解
执行用时:944 ms, 在所有 Python3 提交中击败了43.29%的用户
内存消耗:14 MB, 在所有 Python3 提交中击败了9.52%的用户
from typing import List
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
from collections import Counter
if not s or not words:return []
one_word = len(words[0])
all_len = len(words) * one_word
n = len(s)
words = Counter(words)
res = []
for i in range(0, n - all_len + 1):
tmp = s[i:i+all_len]
c_tmp = []
for j in range(0, all_len, one_word):
c_tmp.append(tmp[j:j+one_word])
if Counter(c_tmp) == words:
res.append(i)
return res
"""
For Example: input: s = "barfoothefoobarman", words = ["foo","bar"]
output: [0,9]
"""
s = "barfoothefoobarman"
words = ["foo","bar"]
solution = Solution()
result = solution.findSubstring(s, words)
print('输出为:',result)
方法二: 滑动窗口!我们一直在 s 维护着所有单词长度总和的一个长度队列!时间复杂度:O(n)题解
执行用时:164 ms, 在所有 Python3 提交中击败了77.36%的用户
内存消耗:13.8 MB, 在所有 Python3 提交中击败了9.52%的用户
from typing import List
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
from collections import Counter
if not s or not words:return []
one_word = len(words[0])
word_num = len(words)
n = len(s)
words = Counter(words)
res = []
for i in range(0, one_word):
cur_cnt = 0
left = i
right = i
cur_Counter = Counter()
while right + one_word <= n:
w = s[right:right + one_word]
right += one_word
cur_Counter[w] += 1
cur_cnt += 1
while cur_Counter[w] > words[w]:
left_w = s[left:left+one_word]
left += one_word
cur_Counter[left_w] -= 1
cur_cnt -= 1
if cur_cnt == word_num :
res.append(left)
return res
"""
For Example: input: s = "barfoothefoobarman", words = ["foo","bar"]
output: [0,9]
"""
s = "barfoothefoobarman"
words = ["foo","bar"]
solution = Solution()
result = solution.findSubstring(s, words)
print('输出为:',result)
方法三: 该方法是对方法二的优化, 加入一定的剪枝 题解
执行用时:80 ms, 在所有 Python3 提交中击败了91.01%的用户
内存消耗:13.9 MB, 在所有 Python3 提交中击败了9.52%的用户
from typing import List
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
from collections import Counter
if not s or not words:return []
one_word = len(words[0])
word_num = len(words)
n = len(s)
if n < one_word:return []
words = Counter(words)
res = []
for i in range(0, one_word):
cur_cnt = 0
left = i
right = i
cur_Counter = Counter()
while right + one_word <= n:
w = s[right:right + one_word]
right += one_word
if w not in words:
left = right
cur_Counter.clear()
cur_cnt = 0
else:
cur_Counter[w] += 1
cur_cnt += 1
while cur_Counter[w] > words[w]:
left_w = s[left:left+one_word]
left += one_word
cur_Counter[left_w] -= 1
cur_cnt -= 1
if cur_cnt == word_num :
res.append(left)
return res
"""
For Example: input: s = "barfoothefoobarman", words = ["foo","bar"]
output: [0,9]
"""
s = "barfoothefoobarman"
words = ["foo","bar"]
solution = Solution()
result = solution.findSubstring(s, words)
print('输出为:',result)
参考
- https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/solution/chuan-lian-suo-you-dan-ci-de-zi-chuan-by-powcai/