-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidPalindrome.java
92 lines (84 loc) · 3.07 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package learn.freq05;
//检查一个string是否是回文 eg:racecar
//also we define empty string as valid palindrome.
//"a." 是回文 因为 considering only alphanumeric characters and ignoring cases.
public class ValidPalindrome {
public boolean isPalindrome(String s) {
if ((s.trim()).equals("") || s == null) {
return true;
}
// STRING 先要处理 去除非alphanueric的 和空格
String temp = s.toLowerCase();
// for (int i = 0; i < temp.length(); i++) {
// if (!((temp.charAt(i) >= '0' && temp.charAt(i) <= '9') || (temp
// .charAt(i) >= 'a' && temp.charAt(i) <= 'z'))) {
// temp.replace(temp.charAt(i), ' ');
// System.err.print(i);
// }
// }
// System.err.print(temp);
// String withoutSpaceString = temp.replace("$", "");
//
// withoutSpaceString = withoutSpaceString.replace(" ", "");
int start = 0;
int end = temp.length() - 1;
while (start <= end) {
if (((temp.charAt(start) >= '0' && temp.charAt(start) <= '9') || (temp
.charAt(start) >= 'a' && temp.charAt(start) <= 'z'))
&& ((temp.charAt(end) >= '0' && temp.charAt(end) <= '9') || (temp
.charAt(end) >= 'a' && temp.charAt(end) <= 'z'))) {
if (temp.charAt(start) != temp.charAt(end)) {
return false;
}
start++;
end--;
} else {
if (!((temp.charAt(start) >= '0' && temp.charAt(start) <= '9') || (temp
.charAt(start) >= 'a' && temp.charAt(start) <= 'z'))) {
start++;
}
if (!((temp.charAt(end) >= '0' && temp.charAt(end) <= '9') || (temp
.charAt(end) >= 'a' && temp.charAt(end) <= 'z'))) {
end--;
}
}
}
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print((new ValidPalindrome()).isPalindrome("avavba"));
System.out.print((new ValidPalindrome()).isPalindrome2("ab"));
}
public boolean isPalindrome2(String s) {
if (s == null) {
return false;
}
if (s.length() == 0 || s.trim().equals("")) {
return true;
}
s = s.toLowerCase();
StringBuilder chars = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z' || s.charAt(i) >= '0' && s.charAt(i) <= '9') {
chars.append(s.charAt(i));
}
}
String string = chars.toString();
System.err.println(string);
if (string.length() == 0) {
return true;
}
int start = 0;
int end = string.length() - 1;
System.err.println(start + "," + end);
while (start <= end) {
if (string.charAt(start) != string.charAt(end)) {
return false;
}
start++;
end--;
}
return true;
}
}