Skip to content

Commit 19d9657

Browse files
committed
add solution for valid-palindrome
1 parent 9a3a016 commit 19d9657

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

valid-palindrome/Ujoonnee.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// time complexity : O(n)
2+
// space complexity : O(1)
3+
4+
class Solution {
5+
public boolean isPalindrome(String s) {
6+
s = s.toLowerCase();
7+
int i = 0;
8+
int j = s.length() - 1;
9+
while(i < j) {
10+
char c1 = s.charAt(i);
11+
if (!isAlphanumeric(c1)) {
12+
i++;
13+
continue;
14+
}
15+
16+
char c2 = s.charAt(j);
17+
if (!isAlphanumeric(c2)) {
18+
j--;
19+
continue;
20+
}
21+
22+
if (c1 != c2) {
23+
return false;
24+
}
25+
i++;
26+
j--;
27+
}
28+
29+
return true;
30+
}
31+
32+
private boolean isAlphanumeric(char c) {
33+
return (c >= '0' && c<='9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c<= 'z');
34+
}
35+
}
36+
37+
/*
38+
class Solution {
39+
public boolean isPalindrome(String s) {
40+
s = s.toLowerCase();
41+
String[] split = s.replaceAll("[^A-Za-z0-9]", "").split("");
42+
for (int i=0; i<split.length/2; i++) {
43+
if (!split[i].equals(split[split.length-1-i])) {
44+
return false;
45+
}
46+
}
47+
48+
return true;
49+
}
50+
}
51+
*/

0 commit comments

Comments
 (0)