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

作者 : admin 本文共432个字,预计阅读时间需要2分钟 发布时间: 2024-06-6 共2人阅读

【leetcode–30.串联所有单词的子串】插图

        有没有一样喜欢看示例的,,看题目就觉得很难懂。大致就是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

本站无任何商业行为
个人在线分享 » 【leetcode–30.串联所有单词的子串】
E-->