-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-125-Valid-Palindrome.java
More file actions
107 lines (86 loc) · 3.18 KB
/
LeetCode-125-Valid-Palindrome.java
File metadata and controls
107 lines (86 loc) · 3.18 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
LeetCode: https://leetcode.com/problems/valid-palindrome/
LintCode: http://www.lintcode.com/problem/valid-palindrome/
JiuZhang: http://www.jiuzhang.com/solutions/valid-palindrome/
ProgramCreek: http://www.programcreek.com/2013/01/leetcode-valid-palindrome-java/
Analysis:
*/
public class Solution {
// 1. Two Pointers
public boolean isPalindrome(String s) {
if(s == null || s.length() == 0) return true;
// \s is whitespace in regex, and we need \\ to express \
s = s.replaceAll("[^a-zA-Z0-9]", "");
s = s.toLowerCase();
int lo = 0;
int hi = s.length() - 1;
while(lo < hi){
if(s.charAt(lo) != s.charAt(hi)) return false;
lo++;
hi--;
}
return true;
}
// 2. Two Pointers
public boolean isPalindrome(String s) {
if (s == null || s.length() == 0) return true;
int lo = 0, hi = s.length() - 1;
while (lo < hi) {
if (!Character.isLetterOrDigit(s.charAt(lo))) {
lo++;
} else if (!Character.isLetterOrDigit(s.charAt(hi))) {
hi--;
} else {
if (Character.toLowerCase(s.charAt(lo)) != Character.toLowerCase(s.charAt(hi))) return false;
lo++;
hi--;
}
}
return true;
}
// Another Two Pointers
public boolean isPalindrome(String s) {
int lo = 0, hi = s.length() - 1;
while (lo < hi) {
while(lo < hi && !isValid(s.charAt(lo))) lo++;
while(lo < hi && !isValid(s.charAt(hi))) hi--;
if (lo < hi) {
if (!isPalindrome(s.charAt(lo), s.charAt(hi))) return false;
}
lo++;
hi--;
}
return true;
}
private boolean isValid(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
}
private boolean isPalindrome(char c1, char c2) {
return Character.toLowerCase(c1) == Character.toLowerCase(c2);
}
// 3.Reverse and Compare
// https://leetcode.com/problems/valid-palindrome/discuss/39981/My-three-line-java-solution
// public boolean isPalindrome(String s) {
// String actual = s.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
// String rev = new StringBuffer(actual).reverse().toString();
// return actual.equals(rev);
// }
// 4.
public boolean isPalindrome(String s) {
s = s.toLowerCase();
int lo = 0, hi = s.length() - 1;
while (lo < hi) {
while (lo < hi && !(s.charAt(lo) >= 'a' && s.charAt(lo) <= 'z') && !(s.charAt(lo) >= '0' && s.charAt(lo) <= '9')) {
lo++;
}
while(lo < hi && !(s.charAt(hi) >= 'a' && s.charAt(hi) <= 'z') && !(s.charAt(hi) >= '0' && s.charAt(hi) <= '9')) {
hi--;
}
System.out.println(lo + " " + hi);
if (s.charAt(lo) != s.charAt(hi)) return false;
lo++;
hi--;
}
return true;
}
}