LeetCode Valid Palindrome Solution Explained - Java

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


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

These short, sweet, and crisp explanations are better than long 21 minutes explanations.

nikhilmishra
Автор

Hello everyone! I think this solution would be more optimized. It uses less memory and might even be slightly faster (because we don't need to create an entire array, we just iterate over the original String).

public boolean isPalindromeOptimized(String s) {
int left = 0;
int right = s.length() - 1;

while (left < right) {
if {
left++;
continue;
}
if {
right--;
continue;
}

if != {
return false;
}

left++;
right--;
}

return true;
}

awesomebearaudiobooks
Автор

Much better than the actual leetcode explanation. Better solution too! Thank you

mtninjas
Автор

Why not use pointers and skip the non alphanumeric characters when moving closer to each other?

radudanalmasan
Автор

for removing all punctuation and spaces u can directly use regular expression and replace it
s.replaceAll("[^a-zA-Z0-9]", "");

saurabhdeokar
Автор

You could have used StringBuilder instead of String for fixed_string.

TuhinSh
Автор

Loved the way you code. Thanks for the video. Used StringBuilder to optimize it. Thanks

ashishburnwal
Автор

If you guys have better, more optimized solutions in mind, why don't you guys share it here so that others can debate, discuss, and learn? Please consider it if possible.

chocolateandmath
Автор

This can be heavily optimised further.

adityasrivastava
Автор

Time limit exceeded with the solution in the video.

Below code passed.

class Solution {
public boolean isPalindrome(String s) {
List<Character> list = new ArrayList<>();

for (char c : s.toCharArray()) {
if (Character.isDigit(c) || Character.isLetter(c)) {

}
}

char[] clean = new char[list.size()];

for (int j = 0; j < clean.length; j++) {
clean[j] = list.get(j);
}

for (int i = 0; i < clean.length; i++) {
if (clean[i] != clean[clean.length - 1 - i]) {
return false;
}
}

return true;
}
}

candichiu
Автор

can also just use two pointers without the `fixed_string` and if { left++; continue} and then right--; continue; that way it just skips the left char or the right char if its not valid and then checks again

abetts
Автор

That was nice, although I have a question, why didn't you use the "isAlphabetic" function instead of using both "isDigit" & "isLetter" functions ?

dev_troy
Автор

Use StringBuilder when creating the new String.

VIJAYMAMORIA-cm
Автор

wow I was stuck on this for like an hour, but didn't realize I also had to include digits, then I added isDigit and it was right lol

Rob-J-BJJ
Автор

you can do all the chat checking inside while loop itself, and used stringbuilder . then this soln wouldnt be bottom 5%

aabhishek
Автор

This is my solution, let me know if you guys found it helpful.
class Solution {
public boolean isPalindrome(String s) {

s = s.replaceAll("[^a-zA-Z0-9]", "");
s=s.toLowerCase();

StringBuilder resultReverse = new StringBuilder(s);
resultReverse.reverse();
String rev = resultReverse.toString();

if(s.equals(rev))
{
return true;
}
else
{
return false;
}

}
}

luisamiel
Автор

//Time: O(n)
//Space: O(1)
Is this correct?

candichiu
Автор

Thanks for the video!

I share one more shorter solution with 24 ms🔥

class Solution {
public boolean isPalindrome(String s) {

//check if s is empty or null
if(s==" " || s==null){
return true;
}

String newS=s.toLowerCase();

newS=newS.replaceAll("[^a-zA-Z0-9]", "");

newS=newS.replaceAll("\\s", "");

StringBuilder reversed = new


return true;
return false;
}
}

and this is 2 ms🔥🔥
class Solution {
public boolean isPalindrome(String s) {
if (s.isEmpty()) {
return true;
}
int start = 0;
int last = s.length() - 1;
while(start <= last) {
char currFirst = s.charAt(start);
char currLast = s.charAt(last);
if )) {
start++;
} else {
last--;
} else {
if != {
return false;
}
start++;
last--;
}
}
return true;
}
}

אני-שת
Автор

public static boolean isPalindrome(String s) {
s=s.toLowerCase();
s=s.replaceAll("[^A-Za-z0-9]", "");
StringBuilder b = new StringBuilder(s);


b=b.reverse();
if(s.equals(b.toString()) || b.toString().equals(""))
return true;


return false;

}

zoabdullah
Автор

class Solution {
public boolean isPalindrome(String s) {

int head = 0;
int tail = s.length() - 1;

char headc, tailc;

while (head <= tail) {
headc = s.charAt(head);
tailc = s.charAt(tail);

{
head++;
} else if {
tail--;
} else {
if (Character.toLowerCase(headc) != Character.toLowerCase(tailc)) {
return false;
}
head++;
tail--;
}

}
return true;
}
}

julienthyriar
welcome to shbcf.ru