-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0030_substring_with_concatenation.py
More file actions
59 lines (56 loc) · 1.9 KB
/
0030_substring_with_concatenation.py
File metadata and controls
59 lines (56 loc) · 1.9 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
"""
:type s: str
:type words: List[str]
:rtype: List[int]
"""
# Minimum length requirement
wordLen = len(words[0])
minLen = len(words) * wordLen
if (len(s) < minLen):
return []
# ====== 2) Check substring from s using hash table
indexArr = []
wordCount = collections.Counter(words)
for i in range(len(s) - minLen + 1):
remaining = wordCount.copy()
wordsUsed = 0
# Check if subStr is in words
for j in range(i, i + minLen, wordLen):
subStr = s[j:j + wordLen]
if (remaining[subStr] > 0):
remaining[subStr] -= 1
wordsUsed += 1
else:
break
if (wordsUsed == len(words)):
indexArr.append(i)
return indexArr
"""
# ====== 1) Checking all possible concatenations of words
# ====== Time Limit Exceeded
maxCount = -1
for word in words:
count = s.count(word)
if (count == 0):
return []
if (count > maxCount):
maxCount = count
# Check all possible concatenations of words
wordArr = []
count = 0
for word in itertools.permutations(words, len(words)):
index = 0
while (index < len(s)):
index = s.find(''.join(word), index)
if (index < 0):
break
# If concatenation of words is found in s
if (index not in wordArr):
wordArr.append(index)
index += 1
if (maxCount == 1 and len(wordArr) > 0):
return wordArr
return wordArr
"""