-
Notifications
You must be signed in to change notification settings - Fork 21
/
680. Valid Palindrome II.java
executable file
·70 lines (59 loc) · 1.81 KB
/
680. Valid Palindrome II.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
E
tags: String
#### Palindrome String
- delete an index: 有两种情况
- 用一个boolean parameter来表现state. 如果有其他status, state可以变成 String/enum
```
/*
Given a non-empty string s, you may delete at most one character.
Judge whether you can make it a palindrome.
Example 1:
Input: "aba"
Output: True
Example 2:
Input: "abca"
Output: True
Explanation: You could delete the character 'c'.
Note:
The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
*/
class Solution {
public boolean validPalindrome(String s) {
if (s == null || s.length() <= 1) return true;
return validate(s, 0, s.length() - 1, true);
}
public boolean validate(String s, int start, int end, boolean state) {
if (start > end) return false;
while (start < end) {
if (s.charAt(start) == s.charAt(end)) {
start++;
end--;
continue;
} else if (state) {
return validate(s, start + 1, end, false) || validate(s, start, end - 1, false);
}
return false;
}
return true;
}
}
// Simplify start++, end--
class Solution {
public boolean validPalindrome(String s) {
if (s == null || s.length() <= 1) return true;
return validate(s, 0, s.length() - 1, true);
}
public boolean validate(String s, int start, int end, boolean state) {
if (start > end) return false;
while (start < end) {
if (s.charAt(start++) == s.charAt(end--)) {
continue;
} else if (state) {
return validate(s, start - 1, end, false) || validate(s, start, end + 1, false);
}
return false;
}
return true;
}
}
```