Участник:F.Nikitin/FindSubstring — различия между версиями
Материал из DISCOPAL
Phill nik (обсуждение | вклад) (Новая страница: «<code-python> from collections import Counter class Solution: def findSubstring(self, s, words): res = [] if len(words) == 0: return res…») |
StasFomin (обсуждение | вклад) |
||
Строка 1: | Строка 1: | ||
+ | * https://leetcode.com/problems/substring-with-concatenation-of-all-words/ | ||
<code-python> | <code-python> | ||
from collections import Counter | from collections import Counter |
Текущая версия на 11:41, 9 декабря 2019
from collections import Counter class Solution: def findSubstring(self, s, words): res = [] if len(words) == 0: return res word2count = dict(Counter(words)) n, m, k = len(s), len(words[0]), len(words) for i in range(n - m * k + 1): flag = True tmp = {} for j in range(k): word = s[i + m * j:i + m * j + m] if word not in word2count: flag = False break tmp[word] = tmp.get(word, 0) + 1 if tmp[word] > word2count[word]: flag = False break if flag: res.append(i) return res