Minimum String Length After Removing Substrings - Leetcode 2696 - Python

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


0:00 - Read the problem
0:30 - Drawing Explanation
5:48 - Coding Explanation
7:28 - Drawing Explanation 2

leetcode 2696

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

I love that NeetCode has gotten all the milk he needs now and he does the daily vids again.

ArminEghdamiDrums
Автор

Thanks for posting LeetCode daily problems. It's really helpful

shepmax
Автор

I figured the stack solution but the other solution is messy...

deadlyecho
Автор

The slow, but short code to solve this problem is using regex replace.

def minLength(self, s: str) -> int:
while True:
slen = len(s)
s = re.sub("(AB|CD)", "", s)
if slen == len(s):
return slen

The loop breaks when there's no more AB or CD to replace. In this case, regex ran slower than string replace.

AlfredPros
Автор

whats a general solution for a set of strings, and the removable syrings cant be substrings or share paets with each other?

birdbeakbeardneck
Автор

Thanks for the helped to understand that inplace solution
and editorial did fantastic job of explaining the in place solution🙂🙂....

prajwals
Автор

im trying to stay consitent but everyday I come and find neetcode for solutions...I'm I on the right track :(

muskaanfathima
Автор

Strings are immutable in python so the second solution is only good for c++

jaydeeppatel
Автор

i did it recursively
class Solution {
public int minLength(String s) {
return solve(s);
}
public int solve(String s){
for(int i=0; i<s.length()-1; i++){
if( (s.charAt(i)=='A' && s.charAt(i+1)=='B') || (s.charAt(i)=='C' && s.charAt(i+1)=='D') ){
return solve(s.substring(0, i)+s.substring(i+2));
}
}
return s.length();
}
}

anandsahoo
Автор

Please do "1497. Check If Array Pairs Are Divisible by k".

khalidhussien
Автор

Nice man, but pls launch the SQL courses plssss

Lohan_Guedes