Skip to content

Commit 738a2ba

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 2390
1 parent d521ebb commit 738a2ba

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@
264264
- [1922 Count Good Numbers](https://leetcode.com/problems/count-good-numbers/description/)
265265
- [1964 Find the Longest Valid Obstacle Course at Each Position](https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position/description/)
266266
- [2140 Solving Questions With Brainpower](https://leetcode.com/problems/solving-questions-with-brainpower/description/)
267+
- [2390 Removing Stars From a String](https://leetcode.com/problems/removing-stars-from-a-string/description/)
267268
- [2466 Count Ways To Build Good Strings](https://leetcode.com/problems/count-ways-to-build-good-strings/description/)
268269
- [2523 Closest Prime Numbers in Range](https://leetcode.com/problems/closest-prime-numbers-in-range/description/)
269270
- [2799 Count Complete Subarrays in an Array](https://leetcode.com/problems/count-complete-subarrays-in-an-array/description/)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def removeStars(self, s: str) -> str:
5+
"""
6+
You are given a string s, which contains stars *.
7+
8+
In one operation, you can:
9+
- Choose a star in s.
10+
- Remove the closest non-star character to its left, as well as remove the star
11+
itself.
12+
13+
Return the string after all stars have been removed.
14+
15+
Note:
16+
- The input will be generated such that the operation is always possible.
17+
- It can be shown that the resulting string will always be unique.
18+
"""
19+
# Time Complexity: O(n)
20+
# Space Complexity: O(n)
21+
stack = list(s)
22+
res = ""
23+
count = 0
24+
while stack:
25+
c = stack.pop()
26+
if c == "*":
27+
count += 1
28+
elif count == 0:
29+
res = c + res
30+
else:
31+
count -= 1
32+
return res
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._2390_removing_stars_from_a_string import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["s", "expected"],
8+
argvalues=[("leet**cod*e", "lecoe"), ("erase*****", ""), ("abb*cdfg*****x*", "a")],
9+
)
10+
def test_func(s: str, expected: str):
11+
"""Tests the solution of a LeetCode problem."""
12+
removed_stars = Solution().removeStars(s)
13+
assert removed_stars == expected

0 commit comments

Comments
 (0)