-
Notifications
You must be signed in to change notification settings - Fork 1
/
680_Valid_Palindrome_II.cpp
62 lines (56 loc) · 1.45 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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
/**
* @brief The Solution class
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'.
*/
using namespace std;
class Solution {
public:
bool validPalindrome(string &s, size_t start, size_t end) {
while(start < end)
{
if(s.at(start) != s.at(end)) return false;
start++;
end--;
}
return true;
}
bool validPalindrome(string s) {
for(size_t start=0, end=s.size()-1; start<end;)
{
if(s.at(start) != s.at(end))
return validPalindrome(s, start++, end)
|| validPalindrome(s, start, end--);
start++;
end--;
}
return true;
}
void test(string &target, bool ans)
{
bool res = validPalindrome(target);
cout << res << (ans == res ? " == " : " != ") << ans << ":" << target;
cout << endl;
}
};
int main()
{
vector<string> targets = {"aba", "abca","abcdba", "abcdb","cbbcc"};
vector<bool> ans = {true, true, true, false, true};
for(size_t i=0; i<ans.size(); i++)
{
Solution example;
example.test(targets.at(i), ans.at(i));
}
return 0;
}