forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome.java
More file actions
67 lines (53 loc) · 1.38 KB
/
Palindrome.java
File metadata and controls
67 lines (53 loc) · 1.38 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package String;
/**
* Author - archit.s
* Date - 04/10/18
* Time - 11:16 PM
*/
public class Palindrome {
int isAlphaNumeric(String A, int low){
boolean flag = false;
if(A.charAt(low) >= 'A' && A.charAt(low) <= 'Z'){
return 1;
}
else if(A.charAt(low) >= 'a' && A.charAt(low) <= 'z'){
return 2;
}
else if(A.charAt(low) >= '0' && A.charAt(low) <= '9'){
return 3;
}
return 0;
}
public int isPalindrome(String A) {
int low = 0;
int high = A.length()-1;
while(low<=high){
char left, right;
while(low < A.length() && isAlphaNumeric(A,low) == 0){
low++;
}
while(high >= 0 && isAlphaNumeric(A,high) == 0){
high--;
}
if(low == A.length() || high == 0){
return 1;
}
left = A.charAt(low);
right = A.charAt(high);
if(isAlphaNumeric(A,low) == 2){
left = (char)(left - 'a' + 'A');
}
if(isAlphaNumeric(A,high) == 2){
right = (char)(right - 'a' + 'A');
}
if(left != right){
return 0;
}
else{
low++;
high--;
}
}
return 1;
}
}