Skip to content

Commit 9675dc5

Browse files
committed
valid-palindrom solution
1 parent dd0e8ec commit 9675dc5

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

valid-palindrome/hoonjichoi1.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
4+
// removing all non-alphanumeric characters and convert them to lower case
5+
String conveted = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
6+
7+
// early return of the empty string case
8+
if (conveted.length() == 0) {
9+
return true;
10+
}
11+
12+
// check the symmetry
13+
int left = 0, right = conveted.length() - 1;
14+
while (left <= right) {
15+
if (conveted.charAt(left) != conveted.charAt(right)) {
16+
return false;
17+
}
18+
left++;
19+
right--;
20+
}
21+
return true;
22+
}
23+
}

0 commit comments

Comments
 (0)