Skip to content

Commit 9027902

Browse files
refactor 125
1 parent 498abd2 commit 9027902

File tree

1 file changed

+24
-19
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+24
-19
lines changed
Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
package com.fishercoder.solutions;
22

3-
/**Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
3+
/**
4+
* 125. Valid Palindrome
5+
6+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
47
58
For example,
69
"A man, a plan, a canal: Panama" is a palindrome.
710
"race a car" is not a palindrome.
811
912
Note:
1013
Have you consider that the string might be empty? This is a good question to ask during an interview.
14+
For the purpose of this problem, we define empty string as valid palindrome.
15+
*/
1116

12-
For the purpose of this problem, we define empty string as valid palindrome.*/
1317
public class _125 {
1418

19+
public static class Solution1 {
1520
public boolean isPalindrome(String s) {
16-
int i = 0;
17-
int j = s.length() - 1;
18-
char[] chars = s.toCharArray();
19-
while (i < j) {
20-
while (i < j && !Character.isLetterOrDigit(chars[i])) {
21-
i++;
22-
}
23-
while (i < j && !Character.isLetterOrDigit(chars[j])) {
24-
j--;
25-
}
26-
if (Character.toLowerCase(chars[i]) != Character.toLowerCase(chars[j])) {
27-
return false;
28-
}
29-
i++;
30-
j--;
21+
int i = 0;
22+
int j = s.length() - 1;
23+
char[] chars = s.toCharArray();
24+
while (i < j) {
25+
while (i < j && !Character.isLetterOrDigit(chars[i])) {
26+
i++;
27+
}
28+
while (i < j && !Character.isLetterOrDigit(chars[j])) {
29+
j--;
3130
}
32-
return true;
31+
if (Character.toLowerCase(chars[i]) != Character.toLowerCase(chars[j])) {
32+
return false;
33+
}
34+
i++;
35+
j--;
36+
}
37+
return true;
3338
}
34-
39+
}
3540
}

0 commit comments

Comments
 (0)