Skip to content

Commit 93a08fa

Browse files
author
Partho Biswas
committed
1087. Brace Expansion
1 parent da4de8d commit 93a08fa

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ I have solved quite a number of problems from several topics. See the below tabl
267267
|10| [78. Subsets](https://leetcode.com/problems/subsets/)| [Python](leetcode.com/python/78_Subsets.py)| [Video 1](https://www.youtube.com/results?search_query=All+Subsets+of+a+Set), [Video 2](https://www.youtube.com/watch?v=MsHFNGltIxw), [Video 3](https://www.youtube.com/watch?v=xTNFs5KRV_g), [Article 1](https://tinyurl.com/y5p2ozjo) | Medium |📌 TODO: check bit manipulation |
268268
|11| [77. Combinations](https://leetcode.com/problems/combinations/)| [Python](leetcode.com/python/77_Combinations.py)| [Video 1 - paid course](https://codinginterviewclass.com/courses/coding-interview-class/lectures/11725951) | Medium |📌 TODO: Not done. Hard to grasp. Check again |
269269
|12| [1066. Campus Bikes II](https://leetcode.com/problems/campus-bikes-ii/)| [Python](leetcode.com/python/1066_Campus_Bikes_II.py)| [Algo Notes](https://algorithm-notes-allinone.blogspot.com/2019/09/leetcode-1066-campus-bikes-ii.html) | Medium |📌 TODO: Backtracking solution is getting TLE. Solve it and implement it with DP also |
270+
|13| [1087. Brace Expansion](https://leetcode.com/problems/brace-expansion/)| [Python](leetcode.com/python/1087_Brace_Expansion.py)| --- | Medium |📌 --- |
270271

271272

272273
### [Greedy](https://www.youtube.com/watch?v=ARvQcqJ_-NY)
@@ -483,6 +484,9 @@ I have solved quite a number of problems from several topics. See the below tabl
483484

484485
Some helpful links, channels, tutorials, blogs.
485486

487+
<b>Strategies</b>
488+
01. [How a Googler solves coding problems](https://blog.usejournal.com/how-a-googler-solves-coding-problems-ec5d59e73ec5)
489+
486490
<b>Courses/Books</b>
487491
01. [Data Structures in Python: An Interview Refresher](https://www.educative.io/courses/data-structures-in-python-an-interview-refresher)
488492
02. [Grokking the Coding Interview: Patterns for Coding Questions](https://www.educative.io/courses/grokking-the-coding-interview)
@@ -547,6 +551,14 @@ Some helpful links, channels, tutorials, blogs.
547551
07. [Foundation of algorithms - Chapter 5 (Backtracking) notes](https://www.cpp.edu/~ftang/courses/CS331/notes/backtracking.pdf)
548552
08. [Backtracking Search Algorithms](https://cs.uwaterloo.ca/~vanbeek/Publications/survey06.pdf)
549553

554+
<b>Recursion</b>
555+
01. [Learning to think with recursion, part 1](https://medium.com/@daniel.oliver.king/getting-started-with-recursion-f89f57c5b60e)
556+
02. [Learning to think with recursion, part 2](https://medium.com/@daniel.oliver.king/learning-to-think-with-recursion-part-2-887bd4c41274)
557+
03. [What makes a data structure recursive?](https://stackoverflow.com/questions/45761182/what-makes-a-data-structure-recursive)
558+
04. [Binary Tree as a Recursive Data Structure](https://opendsa-server.cs.vt.edu/ODSA/Books/CS3/html/RecursiveDS.html)
559+
05.
560+
561+
550562

551563

552564

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution(object):
2+
def expand(self, S):
3+
"""
4+
:type S: str
5+
:rtype: List[str]
6+
"""
7+
wordList = []
8+
allGeneratedWords = []
9+
self.getWordList(S, wordList)
10+
self.generateWords(wordList, "", allGeneratedWords)
11+
return sorted(allGeneratedWords)
12+
13+
def generateWords(self, wordList, runningWord, allGeneratedWords):
14+
if not wordList:
15+
allGeneratedWords.append(runningWord)
16+
return
17+
word = wordList[0]
18+
for j in range(len(word)):
19+
char = word[j]
20+
runningWord += char
21+
self.generateWords(wordList[1:], runningWord, allGeneratedWords)
22+
runningWord = runningWord[:len(runningWord) - 1]
23+
24+
def getWordList(self, S, wordList):
25+
start = S.find('{')
26+
end = S.find('}')
27+
if start >= 0:
28+
if start > 0:
29+
wordList.append(S[0:start].split(','))
30+
wordList.append(S[start + 1:end].split(','))
31+
self.getWordList(S[end+1:], wordList)
32+
else:
33+
if S:
34+
wordList.append(S.split(','))
35+
return
36+
37+
38+
39+
40+
41+
sol = Solution()
42+
input = "{a,b}{z,x,y}"
43+
output = sol.expand(input)
44+
print('Res: ', output)

0 commit comments

Comments
 (0)