Clear Digits - Leetcode 3174 - Python

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


0:00 - Read the problem
0:30 - Drawing Explanation
4:10 - Coding Explanation

leetcode 3174

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

Where is bro when you need him the most...

anuraggr
Автор

This was a decent problem for the easy tag. Took me some time but when it clicked, it became almost trivial.

yhbarve
Автор

It's been 4 days, where are you?

f
Автор

I used a stack of indices to solve this. If digit, pop from the stack, if char, push onto stack etc. but I like your approaches here! They’re better! I didn’t even brute force, just thought of Stack<Integer> right away! lol 😂 btw I used integer stack for indices, because if the char repeated then string functions to replace in some languages can get tricky!

Amy-
Автор

I'm not sure why reversing.. not critics or anything (Me myself solve it without a stack initially )
This simply worked for me, Iterating with appending to the stack and popping for each digit.

class Solution(object):
def clearDigits(self, s):
"""
:type s: str
:rtype: str
"""
stack = []
for c in s:
if c.isdigit() and stack:
stack.pop()
else:
stack.append(c)
return ''.join(stack)

TruthAlwaysWinn
Автор

Can also check if digit with Regex, with JS:
if (/\d/.test(s[i])) { /* is 0-9 digit */ }

patchouli
Автор

able to come with bruete force approach, but the mention approach seems to be good as the timc complexity is o(n)

jadanitin
Автор

class Solution:
def clearDigits(self, s: str) -> str:

res = []

for c in s:
if not c.isdigit():
res.append(c)
else:
res.pop()

return "".join(map(str, res))

hlebushik
Автор

def clearDigits(self, s):
stack = []

for c in s:
if c.isdigit():
stack and stack.pop()
else:
stack.append(c)

return "".join(stack)

A much simpler and more efficient solution.
Thanks NeetCode <3

AngryBirdie
Автор

What software do you use to draw/scribe?

harshitkumar
Автор

class Solution:
def clearDigits(self, s: str) -> str:

stack = []

for i in range(len(s)):

if not s[i].isdigit():
stack.append(s[i])
else:
stack.pop()

return "".join(stack)

uchiha
Автор

Don't stack based approach is more intuitive than tracking of delete_cnt ?
When both the approaches takes linear space

dhaneshvenkateslu
Автор

what about slicing
ans = ""
for i in range(len(s)):
if not s[i].isdigit():
ans += s[i]
else:
ans = ans[:-1]

return ans

sushipackdushi
Автор

how long did it take you to discover this solution?

faeancestor
Автор

Use stacks it is more efficient i think.

mohammedumair
Автор

This was my solution. Please let me know your thoughts

def clearDigits(self, s: str) -> str:
# iterate in reverse
# use a variable for tracking digit count
# if we encounter digit we increment the digit count
# if we encounter char, we ignore the char if the digit count != 0 and decrement the digit count
# if we encounter char, when digit count is 0, we add it to output string

digit_count = 0
res = ""

for i in range(len(s) - 1, -1, -1):
if s[i].isalpha():
if digit_count == 0:
res = s[i] + res
else:
digit_count -= 1
else:
digit_count += 1

return res

saisundar
Автор

Stack implementation just dry run it one time
string clearDigits(string s) {
string res="";
stack<char>str;
for(auto i:s)
{
if(isalpha(i))
str.push(i);
else
{
if(!str.empty())
str.pop();
}
}
while(!str.empty())
{

res+=str.top();
str.pop();
}
reverse(res.begin(), res.end());
return res;
}

anythinganytime
Автор

public class Solution {
public string ClearDigits(string s) {
StringBuilder sb = new();

for (int i = 0; i < s.Length; i++)
{
char c = s[i];
if (IsValidChar(c))
sb.Append(c);
else
if (sb.Length > 0)
sb.Length--;
}

return sb.ToString();

bool IsValidChar(char c)
=> c >= 97 && c <= 122;
}
}

harrisonwell
Автор

//My Solution in C 🙂:
char* clearDigits(char* s) {
int n = strlen(s), idx = 0, i = 0;
char stack[n];
while(s[i] != '\0')
{
if(isdigit(s[i]) && idx >= 0) {
i++;
if( !idx ) continue;
else idx--;
}
else {
stack[idx] = s[i];
idx++; i++;
}
}

if (idx == 0) return "";
char* res = malloc(idx+1);
for(i = 0; i < idx; i++)
res[i] = stack[i];
res[i] = '\0';
return res;
}

RajivLochanPanda
Автор

class Solution:
def clearDigits(self, s: str) -> str:

res=[]
for i in s:
if i in alpha:
res.append(i)
else:
res.pop()
return ''.join(res)

topeverything
join shbcf.ru