forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidPalindrome.java
39 lines (29 loc) · 910 Bytes
/
ValidPalindrome.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
class ValidPalindrome {
// Need to conside only alpha numeric case => Only Letters and Digist
// In case of character, we need to ignore case
public boolean isPalindrome(String s) {
int start = 0;
int end = s.length() -1;
// 2 pointer Approach
while(start< end){
char sChar = Character.toUpperCase(s.charAt(start));
if(!Character.isLetterOrDigit(sChar)) {
start++;
continue;
}
char eChar = Character.toUpperCase(s.charAt(end));
if(!Character.isLetterOrDigit(eChar)) {
end--;
continue;
}
// Upper cased start and end characters
if(sChar!=eChar){
return false;
} else {
start++;
end--;
}
}
return true;
}
}