Skip to content

Valid Palindrome #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Valid_Palindrome
  • Loading branch information
ifenil committed Oct 3, 2020
commit e25659fe897057ad0f72f0433d21b00525789af7
19 changes: 14 additions & 5 deletions Leetcode/Leetcode_August_Challenge/Valid_Palindrome.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
public class Valid_Palindrome {

public boolean isPalindrome(String s) {

// check string if it's null no need to go further direct return false

if(s==null){
return false;
}


// if string contain some char as uppercase or lowercase
// this will convert whole string to lowercase
s = s.toLowerCase();

// pointed i to intially at 0
// and j to end of the string (last character)
int i=0;
int j=s.length()-1;


while(i<j){
//if i<j and string char contain a-z and 0-9 loop -> true

while(i<j && !((s.charAt(i)>='a' && s.charAt(i)<='z')
|| (s.charAt(i)>='0'&&s.charAt(i)<='9'))){
i++;
Expand All @@ -23,15 +31,16 @@ public boolean isPalindrome(String s) {
|| (s.charAt(j)>='0'&&s.charAt(j)<='9'))){
j--;
}

// if one of string char not match i to j
// it will return false
if(s.charAt(i) != s.charAt(j)){
return false;
}

i++;
j--;
}

// else return true
return true;

}
Expand Down