-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathPalindrome.java
44 lines (40 loc) · 1.24 KB
/
Palindrome.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
public class Palindrome {
public Deque<Character> wordToDeque(String word) {
Deque<Character> res = new ArrayDeque<>();
for (int i = 0; i < word.length(); i++) {
res.addLast(word.charAt(i));
}
return res;
}
public boolean isPalindrome(String word) {
Deque<Character> d = wordToDeque(word);
while (d.size() > 1) {
if (d.removeFirst() != d.removeLast()) {
return false;
}
}
return true;
}
public boolean isPalindrome(String word, CharacterComparator cc) {
Deque<Character> d = wordToDeque(word);
while (d.size() > 1) {
if (!cc.equalChars(d.removeFirst(), d.removeLast())) {
return false;
}
}
return true;
}
// public boolean isPalindromeRecursive(String word) {
// Deque<Character> d = wordToDeque(word);
// return isPalindromeRecursive(d);
// }
// private boolean isPalindromeRecursive(Deque<Character> d) {
// if (d.size() <= 1) {
// return true;
// }
// if (d.removeFirst() != d.removeLast()) {
// return false;
// }
// return isPalindromeRecursive(d);
// }
}