? ? ? ? 有沒有一樣喜歡看示例的,,看題目就覺得很難懂。大致就是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