-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidPalindrome.java
40 lines (35 loc) · 1.16 KB
/
ValidPalindrome.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.nphausg.leetcode.easy;
import com.nphausg.leetcode.config.BaseTest;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* <a href="https://leetcode.com/problems/valid-palindrome">125. Valid Palindrome</a>
*/
@RunWith(Enclosed.class)
public class ValidPalindrome {
public static boolean isPalindrome(String s) {
int left = 0, right = s.length() - 1;
while (left < right) {
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
left++;
}
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
right--;
}
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
return false;
}
left++;
right--;
}
return true;
}
public static class TestCase extends BaseTest {
@org.junit.Test
public void testCases() {
assertTrue(isPalindrome("A man, a plan, a canal: Panama"));
assertFalse(isPalindrome("race a car"));
}
}
}