-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathValid_Palindrome.java
47 lines (36 loc) Β· 1.29 KB
/
Valid_Palindrome.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package Leetcode_August_Challenge;
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++;
}
while(i<j && !((s.charAt(j)>='a' && s.charAt(j)<='z')
|| (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;
}
}