Skip to content

Commit ff90fff

Browse files
committed
Time: 106 ms (86.75%), Space: 19.9 MB (90.17%) - LeetHub
1 parent bdfad6d commit ff90fff

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# time complexity: O(2^n)
2+
# space complexity: O(n)
3+
from typing import List
4+
5+
6+
class Solution:
7+
def storeAbbreviations(
8+
self,
9+
abbreviations: List[str],
10+
word: str,
11+
currWord: str,
12+
index: int,
13+
abbreviatedCount: int
14+
):
15+
if index == len(word):
16+
if abbreviatedCount > 0:
17+
currWord += str(abbreviatedCount)
18+
abbreviations.append(currWord)
19+
return
20+
21+
self.storeAbbreviations(
22+
abbreviations,
23+
word,
24+
currWord
25+
+ (str(abbreviatedCount) if abbreviatedCount > 0 else "")
26+
+ word[index],
27+
index + 1,
28+
0
29+
)
30+
31+
self.storeAbbreviations(
32+
abbreviations, word, currWord, index + 1, abbreviatedCount + 1
33+
)
34+
35+
def generateAbbreviations(self, word: str) -> List[str]:
36+
abbreviations = []
37+
self.storeAbbreviations(abbreviations, word, "", 0, 0)
38+
return abbreviations
39+
40+
41+
word = "word"
42+
print(Solution().generateAbbreviations(word))

0 commit comments

Comments
 (0)