Skip to content

Commit 2318676

Browse files
update 125
1 parent 89a5fb0 commit 2318676

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

src/main/java/com/fishercoder/solutions/_125.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
public class _125 {
44
public static class Solution1 {
55
public boolean isPalindrome(String s) {
6-
int i = 0;
7-
int j = s.length() - 1;
6+
int left = 0;
7+
int right = s.length() - 1;
88
char[] chars = s.toCharArray();
9-
while (i < j) {
10-
while (i < j && !Character.isLetterOrDigit(chars[i])) {
11-
i++;
9+
while (left < right) {
10+
while (left < right && !Character.isLetterOrDigit(chars[left])) {
11+
left++;
1212
}
13-
while (i < j && !Character.isLetterOrDigit(chars[j])) {
14-
j--;
13+
while (left < right && !Character.isLetterOrDigit(chars[right])) {
14+
right--;
1515
}
16-
if (Character.toLowerCase(chars[i]) != Character.toLowerCase(chars[j])) {
16+
if (Character.toLowerCase(chars[left]) != Character.toLowerCase(chars[right])) {
1717
return false;
1818
}
19-
i++;
20-
j--;
19+
left++;
20+
right--;
2121
}
2222
return true;
2323
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._125;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _125Test {
10+
private static _125.Solution1 solution1;
11+
private static String s;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _125.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
s = "A man, a plan, a canal: Panama";
21+
assertEquals(true, solution1.isPalindrome(s));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
s = "race a car";
27+
assertEquals(false, solution1.isPalindrome(s));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
s = " ";
33+
assertEquals(true, solution1.isPalindrome(s));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
s = "0P";
39+
assertEquals(false, solution1.isPalindrome(s));
40+
}
41+
}

0 commit comments

Comments
 (0)