|
2 | 2 |
|
3 | 3 | ## Initial thoughts (stream-of-consciousness)
|
4 | 4 |
|
| 5 | +- I can think of a few potential ways to solve this: |
| 6 | + - a recursive solution that'll entail checking whether `s` contains parentheses and if so, calling `reverseParentheses` on the substring between the outermost pair of parentheses before reversing `s` |
| 7 | + - a solution where we build up the output string by concatenating letters from either the beginning or end of `s`, switching back and forth between the two ends when we encounter open/close parentheses (switch from forward to backward if the running count of `(` is odd) |
| 8 | +- The recursive solution seems simpler to me, so I'll start with that. I think it'll probably be less efficient than the single-pass, forward/reverse version though. |
| 9 | +- Recursive solution: |
| 10 | + - check if `s` contains parenthesis |
| 11 | + - if no (base case), return `s[::-1]` |
| 12 | + - if yes (recursive case): |
| 13 | + - extract substring before first `(`, call this `before` |
| 14 | + - extract substring after last `)`, call this `after` |
| 15 | + - call `reverseParentheses` on substring between first `(` and last `)`, call the result `new_substr` |
| 16 | + - return `f"{before}{new_substr}{after}"[::-1]` |
| 17 | + - actually... all of the test cases happen to have the full string surrounded by parentheses, but this doesn't seem to be guaranteed, so I don't necessarily want to reverse the *whole* string at the end (outermost call). So I think I'll need to reverse each substring "one level up" from the call in which it's `s` -- i.e., my base case should just return `s` and my recursive case should return `f"{before}{new_substr[::-1]}{after}"` |
| 18 | + - what if we have a string containing `()`? Could add a 3rd condition that just returns `s` if `s` is an empty string, but probably not worth it because the base case will return the right result anyways. |
| 19 | + |
5 | 20 | ## Refining the problem, round 2 thoughts
|
6 | 21 |
|
7 | 22 | ## Attempted solution(s)
|
| 23 | + |
| 24 | +### First attempt |
| 25 | + |
8 | 26 | ```python
|
9 |
| -class Solution: # paste your code here! |
10 |
| - ... |
| 27 | +class Solution: |
| 28 | + def reverseParentheses(self, s: str) -> str: |
| 29 | + if "(" in s: |
| 30 | + open_ix = s.index("(") |
| 31 | + close_ix = s.rindex(")") |
| 32 | + return f"{s[:open_ix]}{self.reverseParentheses(s[open_ix+1:close_ix])[::-1]}{s[close_ix+1:]}" |
| 33 | + return s |
11 | 34 | ```
|
| 35 | + |
| 36 | +Failed on test case 14, `s = "ta()usw((((a))))"` -- I didn't account for the fact that not all parentheses will necessarily be nested inside each other, so the first `(` won't necessarilly be paired with the last `)` 🤦 |
| 37 | + |
| 38 | +### Second attempt |
| 39 | + |
| 40 | +Okay then, in that case, I don't think a recursive solution will work. But I think I can solve it a similar way as what I was thinking, using a `while` loop instead: |
| 41 | + |
| 42 | +- while `s` contains parentheses: |
| 43 | + - find the index of the last (rightmost) `(` |
| 44 | + - find the index of the first (leftmost) `)` *that appears **after** the last (rightmost) `(`* |
| 45 | + - set `s` equal to the substring up to the last `(`, plus the substring between that and the first subsequent `)`, plus the substring after that `)`. |
| 46 | + |
| 47 | +```python |
| 48 | +class Solution: |
| 49 | + def reverseParentheses(self, s: str) -> str: |
| 50 | + while "(" in s: |
| 51 | + open_ix = s.rindex("(") |
| 52 | + close_ix = s.index(")", open_ix) |
| 53 | + s = f"{s[:open_ix]}{s[open_ix+1:close_ix][::-1]}{s[close_ix+1:]}" |
| 54 | + return s |
| 55 | +``` |
| 56 | + |
| 57 | + |
0 commit comments