-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckpalindrome.java
63 lines (51 loc) · 1.7 KB
/
checkpalindrome.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// import java.util.*;
public class checkpalindrome {
public static void main(String[] args) {
System.out.println(check("A man, a plan, a canal: Panama"));
}
public static boolean check(String str) {
String output = "";
String check = "";
str = str.toLowerCase();
int ind = 0;
for (int i = str.length() - 1; i >= 0; i--) {
if (((int) str.charAt(i) >= 97 && (int) str.charAt(i) <= 122)
|| ((int) str.charAt(i) >= 48 && (int) str.charAt(i) <= 57)) {
output += str.charAt(i);
}
if (((int) str.charAt(ind) >= 97 && (int) str.charAt(ind) <= 122)
|| ((int) str.charAt(ind) >= 48 && (int) str.charAt(ind) <= 57)) {
check += str.charAt(ind);
}
ind++;
}
System.out.println(output);
System.out.println(check);
if (output.equals(check)) {
return true;
} else {
return false;
}
}
}
// Most Optimised
// int left = 0, right = str.length() - 1;
// while (left < right) {
// // Move the left pointer to the next alphanumeric character
// while (left < right && !Character.isLetterOrDigit(str.charAt(left))) {
// left++;
// }
// // Move the right pointer to the previous alphanumeric character
// while (left < right && !Character.isLetterOrDigit(str.charAt(right))) {
// right--;
// }
// // Compare characters at the current positions (ignoring case)
// if (Character.toLowerCase(str.charAt(left)) !=
// Character.toLowerCase(str.charAt(right))) {
// return false; // Not a palindrome
// }
// // Move both pointers towards the center
// left++;
// right--;
// }
// return true;