Make The String Great - Leetcode 1544 - Python

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


0:00 - Read the problem
0:15 - Drawing Explanation
4:41 - Coding Explanation

leetcode 1544

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

I don't know but i didn't able to solve it with just loop because there are some cases when string becomes bad after removing character so have to check that also for which stack is perfect don't know if you can solve it just by loop.

AnordinaryMan
Автор

You could also check if the difference between the character and and the top of the stack is either the lower case or upper case version of the char by doing abs(ord(char) - ord(stack[-1])) == 32. This works as the ASCII character difference between the upper and lower case versions of the char is always 32.

kevinhklee
Автор

Great! You even implemented a custom version of the str().lower() function; 8:59 however, you may not have had the correct "if" condition. A more accurate condition would be to say "if ord('a') > ord(c) >= ord('A'):", then convert the character to lowercase. Try your implementation with any numerical character and compare it with the str().lower() function; you'll instantly see why this works if you look at an ASCII chart. I'm not meaning to brag, you probably fixed this error yourself; but just in case anybody types that and is like "Why are these two functions giving different results??" I may have an answer.

JLSXMK
Автор

class Solution {
public String makeGood(String s) {
Stack<Character> stack = new Stack();

for(char c : s.toCharArray()) {
if(!stack.isEmpty() && areOppositeCases(stack.peek(), c)) {
stack.pop(); // they cancel each other out
} else {
stack.push(c); // keep the character
}
}

StringBuilder result = new StringBuilder();

for(char c:stack) {
result.append(c);
}

return result.toString();
}

private boolean areOppositeCases(char a, char b) {
return Math.abs(a - b) == 32;
}
}

kaankahveci
Автор

this daily LC solutions are good to keep on track.

theSeniorSDE
Автор

just saw your explanation and came up with this code:

class Solution:
def makeGood(self, s: str) -> str:
st=[s[0]]
for i in range(1, len(s)):
if len(st) and st[-1]!=s[i]:
if
st.pop()
else:
st.append(s[i])
else:
st.append(s[i])
return ''.join(st)

BurhanAijaz
Автор

can we just put the if condition like:

if abs(ord(stack[-1])-s[i])==32:

rohitkumaram
Автор

class Solution:
def makeGood(self, s: str) -> str:
stack=[s[0]]
for i in range(1, len(s)):
if(stack and s[i]!=stack[-1] and
stack.pop()
else:
stack.append(s[i])
return "".join(stack)

k.k.harjeeth
Автор

class Solution:
def makeGood(self, s: str) -> str:
stack = []
for letter in s:
if not stack:
stack.append(letter)
continue
prev = stack[-1]

if abs(ord(prev) - ord(letter)) == 32:
stack.pop()
else:
stack.append(letter)

return "".join(stack)

#Time Complexity: O(n)
#Space Complexity: O(n)

ferbo
Автор

Problems using only ASCII characters should be forbidden. They only train you not to consider all Unicode characters, leading to bugs and potentially security vulnerabilities.

redorange
Автор

how do u handle string becoming bad again after removal bcz next char appearing is capital of what u kept initially, no thought on this, ?

firstyfirst
Автор

Why not use the built in islower isupper functions?

ecchioni
Автор

you took the time to implement the lower function from scratch, but didnt mention that any uppercase - lowercase of the same letter will always give you 32 which is a neat ascii trick
So in the final solution all you had to do was check if abs(ord(stack[-1]) - ord(s[i]) == 32.) Then you dont even have to check if theyre uppercase or lowercase:

stack = []

for c in s:
if stack and abs(ord(stack[-1]) - ord(c)) == 32:
stack.pop()
else:
stack.append(c)

return "".join(stack)

adamgerber
Автор

Why not just check that the absolute value of the ASCII value difference is 32?

OrphanedZombie
Автор

Do you come up with the solution yourself or are you looking at the solutions?

TFShows
welcome to shbcf.ru