1
+ package Algorithms .string ;
2
+
3
+ public class IsPalindrome_2014_1229 {
4
+ /*
5
+ SOLUTION 1: Iterator.
6
+ */
7
+ public boolean isPalindrome1 (String s ) {
8
+ if (s ==null ) {
9
+ return false ;
10
+ }
11
+
12
+ int len =s .length ();
13
+
14
+ int left =0 ;
15
+ int right =len -1 ;
16
+
17
+ String sNew =s .toLowerCase ();
18
+
19
+ while (left <right ) {
20
+ // bug 1: forget a )
21
+ while (left <right && !isNumChar (sNew .charAt (left ))) {
22
+ left ++;
23
+ }
24
+
25
+ while (left <right && !isNumChar (sNew .charAt (right ))) {
26
+ right --;
27
+ }
28
+
29
+ if (sNew .charAt (left ) !=sNew .charAt (right )) {
30
+ return false ;
31
+ }
32
+
33
+ left ++;
34
+ right --;
35
+ }
36
+
37
+ return true ;
38
+ }
39
+
40
+ public boolean isNumChar (char c ) {
41
+ if (c <='9' &&c >='0' ||c <='z' &&c >='a' ||c <='Z' &&c >='A' ) {
42
+ return true ;
43
+ }
44
+
45
+ return false ;
46
+ }
47
+
48
+ /*
49
+ SOLUTION 2: Iterator2.
50
+ */
51
+ public boolean isPalindrome (String s ) {
52
+ if (s ==null ) {
53
+ return false ;
54
+ }
55
+
56
+ int len =s .length ();
57
+
58
+ int left =0 ;
59
+ int right =len -1 ;
60
+
61
+ String sNew =s .toLowerCase ();
62
+
63
+ while (left <right ) {
64
+ // bug 1: forget a )
65
+ if (!Character .isLetterOrDigit (sNew .charAt (left ))) {
66
+ left ++;
67
+ // bug 2: Line 67: error: cannot find symbol: method isLetterOrDigital(char)
68
+ }else if (!Character .isLetterOrDigit (sNew .charAt (right ))) {
69
+ right --;
70
+ }else if (sNew .charAt (left ) !=sNew .charAt (right )) {
71
+ return false ;
72
+ }else {
73
+ left ++;
74
+ right --;
75
+ }
76
+ }
77
+
78
+ return true ;
79
+ }
80
+ }