-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm320.py
36 lines (29 loc) · 1.37 KB
/
m320.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
class Solution:
def generateAbbreviations(self, word: str) -> List[str]:
def dfs(curr: List[str] = [],
currIndx: int = 0,
currNum: int = 0,
output: List[str] = []) -> List[str] :
if currIndx >= len(word) :
output.append(''.join(curr) + ('' if currNum <= 0 else str(currNum)))
return output
# we have numbers in storage
if currNum > 0 :
# include number NOW
curr.append(str(currNum)) # add number
curr.append(word[currIndx]) # add current letter as separator
dfs(curr, currIndx + 1, 0, output)
curr.pop() # pop for space reuse
curr.pop() # pop for space reuse
# wait on number by just adding to existing count
dfs(curr, currIndx + 1, currNum + 1, output)
# we have no numbers in storage
else :
# set current letter as start of abbreviated section
dfs(curr, currIndx + 1, 1, output)
# put in as a letter
curr.append(word[currIndx])
dfs(curr, currIndx + 1, 0, output)
curr.pop() # pop for space reuse
return output
return dfs()