-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path680.valid-palindrome-ii.cpp
80 lines (77 loc) · 1.61 KB
/
680.valid-palindrome-ii.cpp
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
/*
* @lc app=leetcode id=680 lang=cpp
*
* [680] Valid Palindrome II
*
* https://leetcode.com/problems/valid-palindrome-ii/description/
*
* algorithms
* Easy (33.18%)
* Total Accepted: 54.1K
* Total Submissions: 163K
* Testcase Example: '"aba"'
*
*
* 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:
bool validPalindrome(string s) {
int i = 0;
int j = (int)s.size()-1;
while(i<j && s[i]==s[j]){
i++;
j--;
}
if(i >= j) return true;
if(s[i] != s[j-1] && s[i+1] != s[j]) return false;
else if(s[i+1] == s[j] && s[i] == s[j-1]){
int I = i, J = j;
i++;
while(i<j && s[i]==s[j]){
i++;
j--;
}if(i>=j) return true;
i = I, j = J;
j--;
while(i<j && s[i]==s[j]){
i++;
j--;
}if(i>=j) return true;
return false;
}
// Either one matches
if(s[i] == s[j-1]) j--;
else if(s[i+1] == s[j]) i++;
while(i<j && s[i]==s[j]){
i++;
j--;
}
if(i<j) return false;
return true;
}
};