-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0125_isPalindrome.cc
58 lines (51 loc) · 994 Bytes
/
Problem_0125_isPalindrome.cc
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
#include <cctype>
#include <iostream>
#include <string>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
bool validChar(char c) { return std::isalpha(c) || std::isdigit(c); }
bool isEuqal(char c1, char c2) { return (c1 == c2) || ((c1 ^ 32) == c2); }
bool isPalindrome(string s)
{
if (s.length() == 0)
{
return true;
}
int l = 0;
int r = s.length() - 1;
while (l <= r)
{
if (validChar(s[l]) && validChar(s[r]))
{
if (!isEuqal(s[l], s[r]))
{
return false;
}
l++;
r--;
}
else
{
l += validChar(s[l]) ? 0 : 1;
r -= validChar(s[r]) ? 0 : 1;
}
}
return true;
}
};
void testIsPalindrome()
{
Solution s;
EXPECT_TRUE(s.isPalindrome("A man, a plan, a canal: Panama"));
EXPECT_FALSE(s.isPalindrome("race a car"));
EXPECT_TRUE(s.isPalindrome(" "));
EXPECT_SUMMARY;
}
int main()
{
testIsPalindrome();
return 0;
}