Removing Stars From a String - Leetcode 2390 - Python

preview_player
Показать описание

Solving Removing Stars From a String - Leetcode 2390, today's daily leetcode problem on April 10th.

0:00 - Read the problem
0:30 - Drawing Explanation
2:45 - Coding Explanation

leetcode 2390

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

This can be done more trivially without using a stack, traverse the string from the last position, use a count variable to keep count of star characters, if we encounter a non star character and count value > 0, we just decrement the count value, else we would push that character to the resultant string (initially empty), then we just need to reverse the string to get the answer.

saadenaubaje
Автор

This was super easy for a Medium. It took me 3 minutes and half of that was considering edge cases and making sure I didn't miss anything. I thought there would be some catch since it's a Medium.

nikhil_a
Автор

Well I see now where you got your channel name from.
The problem was so simple and your explanation and solution were more simpler.
Damn made me feel I should start leetcoding again.

wagle
Автор

No need to check for Empty stack before pop operation becoz in question it is given that operation is always possible 😅

StoneAge
Автор

Super easy for a medium! I wrote the exact code as you, except one thing. The stack and stack.pop() isn’t needed since it’s generated to always be possible to pop. 😊

vixguy
Автор

Instead of the extra memory of the stack, you could also use pointers to mutate the string in place. One to keep track of where you are in the string for writes, and one to keep track of how far you have read in the string. You just manipulate those pointers and write characters as you parse the string.

Xeoncross
Автор

If we are using the stack, then wouldn't the statement

return "".join(stack)

be non-valid? I understand this is possible since in Python we are using a list to mimic a stack, but in order to keep the stack property, it would need to be appended to a string as such then reversed (all O(n) operations).

res = ''
while stack:
res += stack.pop()

return res[::-1]

Just being nitpicky, but I think mimicking the full property of the stack might be necessary as that is the feature of the data structure of a stack.

qxzsilver
Автор

Are we not taking extra stack space, might use string variables instead?

Eliteshows
Автор

I personally feel using a two pointer approach is better rather than using a stack
hope someone will suggest me better approach that I can use for this code with two pointer

vempatisaivishal
Автор

Thanks bro but I managed this one without you. Getting there!

steeve
Автор

I am here after watching your easy playlist 🌟😊

omaryahia
Автор

Check about empty stack was unnecessary because of preconditions

DeathSugar
Автор

can some one with leetcode premium drop in which company this question is from

bullymaguire
Автор

Question Difficulty - Medium.
Me trying to figure out what's the catch in here lol

encorex
Автор

Literally one regex: s/.\*//g

Assuming it works from left to right, you don't need special code to handle multiple *s in a row.

Alexanderesmith
welcome to shbcf.ru