We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7f1344a commit 7ff138eCopy full SHA for 7ff138e
1 file changed
valid-palindrome/dohyeon2.java
@@ -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