Skip to content

Commit 9c54178

Browse files
committed
August Coding challenge day4
1 parent babca76 commit 9c54178

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.leetcode.AugustChallenge.week1;
2+
3+
public class ValidPalindrome {
4+
public boolean isPalindrome(String s) {
5+
6+
String str = preprocessInputString(s);
7+
8+
int left = 0;
9+
int right = str.length() - 1;
10+
11+
while (left < right) {
12+
if (str.charAt(left) != str.charAt(right))
13+
return false;
14+
left++;
15+
right--;
16+
}
17+
return true;
18+
}
19+
20+
public String preprocessInputString(String s) {
21+
int n = s.length();
22+
s = s.toLowerCase();
23+
String result = "";
24+
for (int i = 0; i < n; i++) {
25+
char ch = s.charAt(i);
26+
if (Character.isLetterOrDigit(ch)) {
27+
result += ch;
28+
}
29+
}
30+
return result;
31+
}
32+
33+
public static void main(String[] args) {
34+
String s = "A man, a plan, a canal: Panama";
35+
System.out.println(new ValidPalindrome().isPalindrome(s));
36+
37+
}
38+
39+
}

0 commit comments

Comments
 (0)