-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringPalindromeChecker.java
More file actions
33 lines (25 loc) · 978 Bytes
/
stringPalindromeChecker.java
File metadata and controls
33 lines (25 loc) · 978 Bytes
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
public class PalindromeChecker {
public boolean isPalindrome(String s) {
class Solution {
public boolean isPalindrome(String s) {
// Normalize the string: convert to lowercase and filter out non-alphanumeric characters
StringBuilder normalized = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isLetterOrDigit(c)) {
normalized.append(Character.toLowerCase(c));
}
}
// Check if the normalized string is a palindrome
String filteredString = normalized.toString();
int left = 0;
int right = filteredString.length() - 1;
while (left < right) {
if (filteredString.charAt(left) != filteredString.charAt(right)) {
return false; // Not a palindrome
}
left++;
right--;
}
return true; // It is a palindrome
}
}