Given a string s that consists of lower case English letters and brackets.
Reverse the strings in each pair of matching parentheses, starting from the innermost one.
Your result should not contain any bracket.
Example 1:
Input: s = "(abcd)"
Output: "dcba"
Example 2:
Input: s = "(u(love)i)"
Output: "iloveu"
Example 3:
Input: s = "(ed(et(oc))el)"
Output: "leetcode"
Example 4:
Input: s = "a(bcdefghijkl(mno)p)q"
Output: "apmnolkjihgfedcbq"
Constraints:
0 <= s.length <= 2000
s only contains lower case English characters and parentheses.
It’s guaranteed that all parentheses are balanced.
Solution: Stack
Use a stack of strings to track all the active strings. Iterate over the input string: 1. Whenever there is a ‘(‘, push an empty string to the stack. 2. Whenever this is a ‘)’, pop the top string and append the reverse of it to the new stack top. 3. Otherwise, append the letter to the string on top the of stack.
Once done, the (only) string on the top of the stack is the answer.
You have an infinite number of stacks arranged in a row and numbered (left to right) from 0, each of the stacks has the same maximum capacity.
Implement the DinnerPlates class:
DinnerPlates(int capacity) Initializes the object with the maximum capacity of the stacks.
void push(int val) pushes the given positive integer val into the leftmost stack with size less than capacity.
int pop() returns the value at the top of the rightmost non-empty stack and removes it from that stack, and returns -1 if all stacks are empty.
int popAtStack(int index) returns the value at the top of the stack with the given index and removes it from that stack, and returns -1 if the stack with that given index is empty.
Explanation:
DinnerPlates D = DinnerPlates(2); // Initialize with capacity = 2
D.push(1);
D.push(2);
D.push(3);
D.push(4);
D.push(5); // The stacks are now: 2 4
1 3 5
﹈ ﹈ ﹈
D.popAtStack(0); // Returns 2. The stacks are now: 4
1 3 5
﹈ ﹈ ﹈
D.push(20); // The stacks are now: 20 4
1 3 5
﹈ ﹈ ﹈
D.push(21); // The stacks are now: 20 4 21
1 3 5
﹈ ﹈ ﹈
D.popAtStack(0); // Returns 20. The stacks are now: 4 21
1 3 5
﹈ ﹈ ﹈
D.popAtStack(2); // Returns 21. The stacks are now: 4
1 3 5
﹈ ﹈ ﹈
D.pop() // Returns 5. The stacks are now: 4
1 3
﹈ ﹈
D.pop() // Returns 4. The stacks are now: 1 3
﹈ ﹈
D.pop() // Returns 3. The stacks are now: 1
﹈
D.pop() // Returns 1. There are no stacks.
D.pop() // Returns -1. There are still no stacks.
Constraints:
1 <= capacity <= 20000
1 <= val <= 20000
0 <= index <= 100000
At most 200000 calls will be made to push, pop, and popAtStack.
Solution: Array of stacks + TreeSet
Store all the stacks in an array, and store the indices of all free stacks in a tree set. 1. push(): find the first free stack and push onto it, if it becomes full, remove it from free set. 2. pop(): pop element from the last stack by calling popAtIndex(stacks.size()-1). 3. popAtIndex(): pop element from given index 3.1 add it to free set if it was full 3.2 remove it from free set if it becomes empty 3.2.1 remove it from stack array if it is the last stack
Time complexity: Push: O(logn) Pop: O(logn) PopAtIndex: O(logn)
You must use only standard operations of a stack — which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
Solution: Use two stacks
amortized cost: O(1)
C++
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
classMyQueue{
public:
/** Initialize your data structure here. */
MyQueue(){}
/** Push element x to the back of queue. */
voidpush(intx){
s1_.push(x);
}
/** Removes the element from in front of queue and returns that element. */
Given an encoded string, return it’s decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won’t be input like 3a or 2[4].
Examples:
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
Solution 1: Recursion
Time complexity: O(n^2) Space complexity: O(n)
C++
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
// Author: Huahua, 4ms
classSolution{
public:
stringdecodeString(strings){
if(s.empty())return"";
stringans;
inti=0;
intn=s.length();
intc=0;
while(isdigit(s[i])&& i < n)
c = c * 10 + (s[i++] - '0');
intj=i;
if(i<n&& s[i] == '[') {
int open = 1;
while(++j<n&& open) {
if (s[j] == '[') ++open;
if(s[j]==']')--open;
}
}else{
while(j<n&& isalpha(s[j])) ++j;
}
// "def2[ac]" => "def" + decodeString("2[ac]")
// | |
// i j
if(i==0)
returns.substr(0,j)+decodeString(s.substr(j));
// "3[a2[c]]ef", ss = decodeString("a2[c]") = "acc"
// | |
// i j
stringss=decodeString(s.substr(i+1,j-i-2));
while(c--)
ans+=ss;
// "3[a2[c]]ef", ans = "abcabcabc", ans += decodeString("ef")