-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubstringwithConcatenationofAllWords.py
More file actions
35 lines (27 loc) · 1.04 KB
/
substringwithConcatenationofAllWords.py
File metadata and controls
35 lines (27 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/substring-with-concatenation-of-all-words/
# Author: Miao Zhang
# Date: 2021-01-07
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
from collections import Counter
if len(words) == 0:
return []
words_dict = Counter(words)
n, lenOfWord, numsOfWord = len(s), len(words[0]), len(words)
res = []
for left in range(n - lenOfWord * numsOfWord + 1):
right = 0
cur_dict = Counter()
while right < numsOfWord:
word = s[left + lenOfWord * right: left + lenOfWord * right + lenOfWord]
if word not in words:
break
cur_dict[word] += 1
if cur_dict[word] > words_dict[word]:
break
right += 1
if right == numsOfWord:
res.append(left)
return res