Skip to content

Commit 3f5ae70

Browse files
committed
day 16
1 parent 4c14ec0 commit 3f5ae70

File tree

2 files changed

+101
-17
lines changed

2 files changed

+101
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Remove All Adjacent Duplicates in String II
3+
============================================
4+
5+
You are given a string s and an integer k, a k duplicate removal consists of choosing k adjacent and equal letters from s and removing them, causing the left and the right side of the deleted substring to concatenate together.
6+
7+
We repeatedly make k duplicate removals on s until we no longer can.
8+
9+
Return the final string after all such duplicate removals have been made. It is guaranteed that the answer is unique.
10+
11+
Example 1:
12+
Input: s = "abcd", k = 2
13+
Output: "abcd"
14+
Explanation: There's nothing to delete.
15+
16+
Example 2:
17+
Input: s = "deeedbbcccbdaa", k = 3
18+
Output: "aa"
19+
Explanation:
20+
First delete "eee" and "ccc", get "ddbbbdaa"
21+
Then delete "bbb", get "dddaa"
22+
Finally delete "ddd", get "aa"
23+
24+
Example 3:
25+
Input: s = "pbbcggttciiippooaais", k = 2
26+
Output: "ps"
27+
28+
Constraints:
29+
1 <= s.length <= 105
30+
2 <= k <= 104
31+
s only contains lower case English letters.
32+
33+
Hint #1
34+
Use a stack to store the characters, when there are k same characters, delete them.
35+
36+
Hint #2
37+
To make it more efficient, use a pair to store the value and the count of each character.
38+
*/
39+
40+
class Solution
41+
{
42+
public:
43+
string removeDuplicates(string str, int k)
44+
{
45+
stack<char> s;
46+
stack<int> counts;
47+
48+
for (int i = 0; i < str.size(); ++i)
49+
{
50+
if (s.size() == 0)
51+
{
52+
s.push(str[i]);
53+
counts.push(1);
54+
}
55+
else if (s.top() == str[i] && counts.top() == k - 1)
56+
{
57+
for (int j = 0; j < k - 1; ++j)
58+
s.pop();
59+
counts.pop();
60+
}
61+
else
62+
{
63+
if (s.top() == str[i])
64+
{
65+
int c = counts.top();
66+
counts.pop();
67+
counts.push(c + 1);
68+
}
69+
else
70+
counts.push(1);
71+
s.push(str[i]);
72+
}
73+
}
74+
75+
string ans;
76+
while (s.size())
77+
{
78+
ans = s.top() + ans;
79+
s.pop();
80+
}
81+
return ans;
82+
}
83+
};
+18-17
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
# April 2021 LeetCoding Challenge
22

3-
| Day | Question Links | Solutions |
4-
| :-: | :--------------------------------------------------------------------------------------------------------------------------------------------------------- | :-----------------------------------------------------------------: |
5-
| 1. | [Palindrome Linked List](https://leetcode.com/explore/featured/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3693/) | [cpp](./01.%20Palindrome%20Linked%20List.cpp) |
6-
| 2. | [Ones and Zeroes](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3694/) | [cpp](./02.%20Ones%20and%20Zeroes.cpp) |
7-
| 3. | [Longest Valid Parentheses](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3695/) | [cpp](./03.%20Longest%20Valid%20Parentheses.cpp) |
8-
| 4. | [Design Circular Queue](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3696/) | [cpp](./04.%20Design%20Circular%20Queue.cpp) |
9-
| 5. | [Global and Local Inversions](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3697/) | [cpp](./05.%20Global%20and%20Local%20Inversions.cpp) |
10-
| 6. | [Minimum Operations to Make Array Equal](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3698/) | [cpp](./06.%20Minimum%20Operations%20to%20Make%20Array%20Equal.cpp) |
11-
| 7. | [Determine if String Halves Are Alike](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3699/) | [cpp](./07.%20Determine%20if%20String%20Halves%20Are%20Alike.cpp) |
12-
| 8. | [Letter Combinations of a Phone Number](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/594/week-2-april-8th-april-14th/3701/) | [cpp](./08.%20Letter%20Combinations%20of%20a%20Phone%20Number.cpp) |
13-
| 9. | [Verifying an Alien Dictionary](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/594/week-2-april-8th-april-14th/3702/) | [cpp](./09.%20Verifying%20an%20Alien%20Dictionary.cpp) |
14-
| 10. | [Longest Increasing Path in a Matrix](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/594/week-2-april-8th-april-14th/3703/) | [cpp](./10.%20Longest%20Increasing%20Path%20in%20a%20Matrix.cpp) |
15-
| 11. | [Deepest Leaves Sum](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/594/week-2-april-8th-april-14th/3704/) | [cpp](./11.%20Deepest%20Leaves%20Sum.cpp) |
16-
| 12. | [Beautiful Arrangement II](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/594/week-2-april-8th-april-14th/3705/) | [cpp](./12.%20Beautiful%20Arrangement%20II.cpp) |
17-
| 13. | [Flatten Nested List Iterator](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/594/week-2-april-8th-april-14th/3706/) | [cpp](./13.%20Flatten%20Nested%20List%20Iterator.cpp) |
18-
| 14. | [Partition List](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/594/week-2-april-8th-april-14th/3707/) | [cpp](./14.%20Partition%20List.cpp) |
19-
| 15. | [Fibonacci Number](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/595/week-3-april-15th-april-21st/3709/) | [cpp](./15.%20Fibonacci%20Number.cpp) |
3+
| Day | Question Links | Solutions |
4+
| :-: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------: |
5+
| 1. | [Palindrome Linked List](https://leetcode.com/explore/featured/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3693/) | [cpp](./01.%20Palindrome%20Linked%20List.cpp) |
6+
| 2. | [Ones and Zeroes](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3694/) | [cpp](./02.%20Ones%20and%20Zeroes.cpp) |
7+
| 3. | [Longest Valid Parentheses](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3695/) | [cpp](./03.%20Longest%20Valid%20Parentheses.cpp) |
8+
| 4. | [Design Circular Queue](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3696/) | [cpp](./04.%20Design%20Circular%20Queue.cpp) |
9+
| 5. | [Global and Local Inversions](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3697/) | [cpp](./05.%20Global%20and%20Local%20Inversions.cpp) |
10+
| 6. | [Minimum Operations to Make Array Equal](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3698/) | [cpp](./06.%20Minimum%20Operations%20to%20Make%20Array%20Equal.cpp) |
11+
| 7. | [Determine if String Halves Are Alike](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3699/) | [cpp](./07.%20Determine%20if%20String%20Halves%20Are%20Alike.cpp) |
12+
| 8. | [Letter Combinations of a Phone Number](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/594/week-2-april-8th-april-14th/3701/) | [cpp](./08.%20Letter%20Combinations%20of%20a%20Phone%20Number.cpp) |
13+
| 9. | [Verifying an Alien Dictionary](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/594/week-2-april-8th-april-14th/3702/) | [cpp](./09.%20Verifying%20an%20Alien%20Dictionary.cpp) |
14+
| 10. | [Longest Increasing Path in a Matrix](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/594/week-2-april-8th-april-14th/3703/) | [cpp](./10.%20Longest%20Increasing%20Path%20in%20a%20Matrix.cpp) |
15+
| 11. | [Deepest Leaves Sum](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/594/week-2-april-8th-april-14th/3704/) | [cpp](./11.%20Deepest%20Leaves%20Sum.cpp) |
16+
| 12. | [Beautiful Arrangement II](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/594/week-2-april-8th-april-14th/3705/) | [cpp](./12.%20Beautiful%20Arrangement%20II.cpp) |
17+
| 13. | [Flatten Nested List Iterator](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/594/week-2-april-8th-april-14th/3706/) | [cpp](./13.%20Flatten%20Nested%20List%20Iterator.cpp) |
18+
| 14. | [Partition List](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/594/week-2-april-8th-april-14th/3707/) | [cpp](./14.%20Partition%20List.cpp) |
19+
| 15. | [Fibonacci Number](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/595/week-3-april-15th-april-21st/3709/) | [cpp](./15.%20Fibonacci%20Number.cpp) |
20+
| 16. | [Remove All Adjacent Duplicates in String II](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/595/week-3-april-15th-april-21st/3710/) | [cpp](./16.%20Remove%20All%20Adjacent%20Duplicates%20in%20String%20II.cpp) |

0 commit comments

Comments
 (0)