赞
踩
有没有一样喜欢看示例的,,看题目就觉得很难懂。大致就是words要进行排列组合,返回s中所有包含这个排列组合的首标。
顺完逻辑蛮好懂的,应该不算困难题,只是不知道用什么模块实现。
- class Solution:
- def findSubstring(self, s: str, words: List[str]) -> List[int]:
- if not s or not words: return []
- one_word = len(words[0])
- all_len = one_word * len(words)
- 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

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。