-
Notifications
You must be signed in to change notification settings - Fork 459
/
Pattern_Matcher.py
46 lines (42 loc) · 1.58 KB
/
Pattern_Matcher.py
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
# O(n^2 + m) time | O(n + m) space
def patternMatcher(pattern, string):
if len(pattern) > len(string):
return []
newPattern = getNewPattern(pattern)
didSwitch = newPattern[0] != pattern[0]
counts = {"x": 0, "y": 0}
firstYPos = getCountsAndFirstYPos(newPattern, counts)
if counts["y"] != 0:
for lenOfX in range(1, len(string)):
lenOfY = (len(string) - lenOfX * counts["x"]) / counts["y"]
if lenOfY <= 0 or lenOfY % 1 != 0:
continue
lenOfY = int(lenOfY)
yIdx = firstYPos * lenOfX
x = string[:lenOfX]
y = string[yIdx: yIdx + lenOfY]
potentialMatch = map(lambda char: x if char == "x" else y, newPattern)
if string == "".join(potentialMatch):
return [x, y] if not didSwitch else [y, x]
else:
lenOfX = len(string) / counts["x"]
if lenOfX % 1 == 0:
lenOfX = int(lenOfX)
x = string[:lenOfX]
potentialMatch = map(lambda char: x, newPattern)
if string == "".join(potentialMatch):
return [x, ""] if not didSwitch else ["", x]
return []
def getNewPattern(pattern):
patternLetters = list(pattern)
if pattern[0] == "x":
return patternLetters
else:
return list(map(lambda char: "x" if char == "y" else "y", patternLetters))
def getCountsAndFirstYPos(pattern, counts):
firstYPos = None
for i, char in enumerate(pattern):
counts[char] += 1
if char == "y" and firstYPos is None:
firstYPos = i
return firstYPos