-
Notifications
You must be signed in to change notification settings - Fork 459
/
Underscorify_Substring.py
62 lines (48 loc) · 1.61 KB
/
Underscorify_Substring.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
47
48
49
50
51
52
53
54
# O(n*m) time | O(n) space
def underscorifySubstring(string, substring):
locations = collapse(getLocations(string, substring))
return underscorify(string, locations)
def getLocations(string, substring):
locations = []
startIndex = 0
while startIndex < len(string):
nextIdx = string.find(substring, startIndex)
if nextIdx != -1:
locations.append([nextIdx, nextIdx + len(substring)])
startIndex = nextIdx + 1
else:
break
return locations
def collapse(locations):
if not len(locations):
return locations
newLocations = [locations[0]]
previous = newLocations[0]
for i in range(1, len(locations)):
current = locations[i]
if current[0] <= previous[1]:
previous[1] = current[1]
else:
newLocations.append(current)
previous = current
return newLocations
def underscorify(string, locations):
locationsIdx = 0
stringIdx = 0
inBetweenUnderscores = False
finalChars = []
i = 0
while stringIdx < len(string) and locationsIdx < len(locations):
if stringIdx == locations[locationsIdx][i]:
finalChars.append("_")
inBetweenUnderscores = not inBetweenUnderscores
if not inBetweenUnderscores:
locationsIdx += 1
i = 0 if i == 1 else 1
finalChars.append(string[stringIdx])
stringIdx += 1
if locationsIdx < len(locations):
finalChars.append("_")
elif stringIdx < len(string):
finalChars.append(string[stringIdx:])
return "".join(finalChars)