-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path125.valid-palindrome.java
More file actions
59 lines (52 loc) · 1.17 KB
/
Copy path125.valid-palindrome.java
File metadata and controls
59 lines (52 loc) · 1.17 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
// @lc code=start
class Solution {
public boolean isPalindrome(String s) {
char[] chList = s.toCharArray();
int left = 0;
int right = s.length() - 1;
while (left <= right) {
while (left < right && !Character.isLetterOrDigit(chList[left])) {
left++;
}
while (left < right && !Character.isLetterOrDigit(chList[right])) {
right--;
}
if (Character.toLowerCase(chList[left]) != Character.toLowerCase(chList[right])) {
return false;
}
left++;
right--;
}
return true;
}
}
// @lc code=end
/*
* @lc app=leetcode id=125 lang=java
*
* [125] Valid Palindrome
*/
// Solution 1
/*
* class Solution {
* public boolean isPalindrome(String s) {
* List<Character> chList = new ArrayList<>();
*
* for (char ch : s.toCharArray()) {
* if (Character.isLetterOrDigit(ch)) {
* chList.add(Character.toLowerCase(ch));
* }
* }
*
* int n = chList.size();
*
* for (int i = 0; i < n; i++) {
* if (chList.get(i) != chList.get(n - 1 - i)) {
* return false;
* }
* }
*
* return true;
* }
* }
*/