|
1 | 1 | packagecom.fishercoder.solutions;
|
2 | 2 |
|
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. |
4 | 7 |
|
5 | 8 | For example,
|
6 | 9 | "A man, a plan, a canal: Panama" is a palindrome.
|
7 | 10 | "race a car" is not a palindrome.
|
8 | 11 |
|
9 | 12 | Note:
|
10 | 13 | 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 | + */ |
11 | 16 |
|
12 |
| - For the purpose of this problem, we define empty string as valid palindrome.*/ |
13 | 17 | publicclass_125 {
|
14 | 18 |
|
| 19 | +publicstaticclassSolution1 { |
15 | 20 | publicbooleanisPalindrome(Strings) {
|
16 |
| -inti =0; |
17 |
| -intj =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 |
| -returnfalse; |
28 |
| - } |
29 |
| -i++; |
30 |
| -j--; |
| 21 | +inti =0; |
| 22 | +intj =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--; |
31 | 30 | }
|
32 |
| -returntrue; |
| 31 | +if (Character.toLowerCase(chars[i]) !=Character.toLowerCase(chars[j])) { |
| 32 | +returnfalse; |
| 33 | + } |
| 34 | +i++; |
| 35 | +j--; |
| 36 | + } |
| 37 | +returntrue; |
33 | 38 | }
|
34 |
| - |
| 39 | + } |
35 | 40 | }
|