forked from lilianweng/LeetcodePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubstr_concat_words.py
More file actions
77 lines (67 loc) · 2.29 KB
/
substr_concat_words.py
File metadata and controls
77 lines (67 loc) · 2.29 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
'''
Leetcode: Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.
For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]
You should return the indices: [0,9].
(order does not matter).
'''
from __future__ import division
import random
# Naive, O(len(S)*len(L)*len(word))
def concat_words(S, L):
k = len(L[0])
rets = []
for i in range(len(S)-k+1):
words = set(L)
start = i
while words:
if S[start:start+k] in words:
words.remove(S[start:start+k])
start += k
else:
break
if not words: rets.append(i)
return rets
# check concatenation at
# first 0, k, 2k, ..., n-1;
# then 1, k+1, 2k+1, ..., n-1;
# then 2, k+2, 2k+2, ..., n-1, etc.;
# until k-1, 2k-1, 3k-1, ..., n-1.
def concat_words2(S,L):
k = len(L[0])
rets = []
for start in range(k):
matched = []
unmatched = set(L)
for i in range(start, len(S)-k+1, k):
# Shift left for k pos
# benefit from the previous match
if matched:
unmatched.add(matched[0])
matched = matched[1:]
# Consider substring starting at i pos
pos = i
while unmatched:
word = S[pos:pos+k]
if word in unmatched: # Continue when matched.
unmatched.remove(word)
matched.append(word)
else:
break # Leave when unmatched.
pos += k
if not unmatched:
rets.append(i)
else:
# clear; prepare for re-matching at the next pos
matched = []
unmatched = set(L)
print rets
return rets
if __name__ == '__main__':
print concat_words("barfoothefoobarman", ["foo","bar"])
print concat_words("skyesskikeyyesskiandkeyskiyes", ["key","ski","yes"])
print concat_words2("barfoothefoobarman", ["foo","bar"])
print concat_words2("skyesskikeyyesskiandkeyskiyes", ["key","ski","yes"])