当前位置:   article > 正文

【leetcode--30.串联所有单词的子串】

【leetcode--30.串联所有单词的子串】

        有没有一样喜欢看示例的,,看题目就觉得很难懂。大致就是words要进行排列组合,返回s中所有包含这个排列组合的首标。

顺完逻辑蛮好懂的,应该不算困难题,只是不知道用什么模块实现。

  1. class Solution:
  2. def findSubstring(self, s: str, words: List[str]) -> List[int]:
  3. if not s or not words: return []
  4. one_word = len(words[0])
  5. all_len = one_word * len(words)
  6. n = len(s)
  7. words = Counter(words)
  8. res = []
  9. for i in range(0, n-all_len+1):
  10. tmp = s[i:i+all_len]
  11. c_tmp = []
  12. for j in range(0, all_len, one_word):
  13. c_tmp.append(tmp[j:j+one_word])
  14. if Counter(c_tmp) == words:
  15. res.append(i)
  16. return res

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号