File tree Expand file tree Collapse file tree 2 files changed +51
-10
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +51
-10
lines changed Original file line number Diff line number Diff line change 3
3
public class _125 {
4
4
public static class Solution1 {
5
5
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 ;
8
8
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 ++;
12
12
}
13
- while (i < j && !Character .isLetterOrDigit (chars [j ])) {
14
- j --;
13
+ while (left < right && !Character .isLetterOrDigit (chars [right ])) {
14
+ right --;
15
15
}
16
- if (Character .toLowerCase (chars [i ]) != Character .toLowerCase (chars [j ])) {
16
+ if (Character .toLowerCase (chars [left ]) != Character .toLowerCase (chars [right ])) {
17
17
return false ;
18
18
}
19
- i ++;
20
- j --;
19
+ left ++;
20
+ right --;
21
21
}
22
22
return true ;
23
23
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments