-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path030_substring_with_concatenation_of_all_words.py
More file actions
53 lines (48 loc) · 1.53 KB
/
030_substring_with_concatenation_of_all_words.py
File metadata and controls
53 lines (48 loc) · 1.53 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
"""
@Question:
You are given a string, s, and a list of words, words, that are
all of the same length. Find all starting indices of substring(s)
in s that is a concatenation of each word in words exactly once
and without any intervening characters.
For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]
You should return the indices: [0,9].
(order does not matter).
"""
class Solution(object):
def all_concentenation(self, words, cons, deep, con, used):
if deep == len(words):
cons.append(con)
else:
for i in range(len(words)):
if i not in used:
used.append(i)
con += words[i]
self.all_concentenation(words, cons, deep+1, con, used)
con = con[0:len(con)-len(words[i])]
used.remove(i)
def findSubstring(self, s, words):
"""
:type s: str
:type words: List[str]
:rtype: List[int]
"""
if not s or not words:
return []
cons = []
deep = 0
con = ""
used = []
self.all_concentenation(words, cons, deep, con, used)
result = []
for con in cons:
index = s.find(con)
if -1 != index and index not in result:
result.append(index)
return result
if __name__ == "__main__":
words = ["foo"]
s = Solution()
result = s.findSubstring("barfoothefoobarman", words)
print result