-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid_palindrome2.cpp
More file actions
65 lines (60 loc) · 1.5 KB
/
valid_palindrome2.cpp
File metadata and controls
65 lines (60 loc) · 1.5 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
60
61
62
63
64
65
/*
* =====================================================================================
*
* Filename: valid_palindrome2.cpp
*
* Description: 680. Valid Palindrome II. Given a non-empty string s, you
* may delete at most one character. Judge whether you can make it a palindrome.
*
* Version: 1.0
* Created: 04/08/19 11:48:42
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <string>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::string;
using std::vector;
// Two pointers
class Solution {
public:
bool validPalindrome(const string& s) {
int i = 0;
int j = s.size() - 1;
while (i < j) {
if (s.at(i) != s.at(j)) {
return isPalindrome(s, i + 1, j) || isPalindrome(s, i, j - 1);
}
i++;
j--;
}
return true;
}
private:
bool isPalindrome(const string& s, int i, int j) {
while (i < j) {
if (s.at(i++) != s.at(j--)) {
return false;
}
}
return true;
}
};
TEST(Solution, validPalindrome) {
vector<std::pair<string, bool>> cases = {
std::make_pair(string("abc"), false),
std::make_pair(string("abca"), true),
std::make_pair(string("aabaca"), true),
};
for (auto& c : cases) {
EXPECT_EQ(Solution().validPalindrome(c.first), c.second);
}
}