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 e14cd76 commit dfaae6cCopy full SHA for dfaae6c
valid-palindrome/GangBean.java
@@ -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