Skip to content

Commit 7ff138e

Browse files
committed
Solution for Valid Palindrome #220
1 parent 7f1344a commit 7ff138e

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

valid-palindrome/dohyeon2.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
// TC : O(n)
3+
// SC : O(1)
4+
public boolean isPalindrome(String s) {
5+
// Condition 1 : after converting all uppercase letters into lowercase letters
6+
// and removing all non-alphanumeric characters
7+
String onlyAlphaNumeric = s.toLowerCase().replaceAll("[^a-z0-9]", "");
8+
int length = onlyAlphaNumeric.length();
9+
for (int i = 0; i < length / 2; i++) {
10+
int right = length - 1 - i;
11+
int left = i;
12+
// Condition 2 : it reads the same forward and backward
13+
if (onlyAlphaNumeric.charAt(left) != onlyAlphaNumeric.charAt(right))
14+
return false;
15+
}
16+
return true;
17+
}
18+
}

0 commit comments

Comments
 (0)