Skip to content

Commit dfaae6c

Browse files
committed
feat: solve valid-palindrome
1 parent e14cd76 commit dfaae6c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

valid-palindrome/GangBean.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
/**
4+
* Step 1: Convert the input string to lowercase. -> O(n)
5+
* Step 2: Remove all non-alphanumeric characters using regex. -> O(n)
6+
* Step 3: Use two pointers to compare characters from both ends of the string. -> O(n)
7+
* Total time complexity: O(n), where n is the length of the string.
8+
*/
9+
s = s.toLowerCase();
10+
s = s.replaceAll("[^a-z0-9]", "");
11+
int left = 0;
12+
int right = s.length() - 1;
13+
if (right <= 0) return true;
14+
while (left < right) {
15+
if (s.charAt(left++) != s.charAt(right--)) return false;
16+
}
17+
return true;
18+
}
19+
}
20+

0 commit comments

Comments
 (0)