-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathsolve.cpp
47 lines (47 loc) · 1003 Bytes
/
solve.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
#include <string>
#include <iostream>
#include <cstdio>
#include <cctype>
using namespace std;
class Solution {
public:
bool isPalindrome(string s) {
int n = s.size();
if (n == 0)
return true;
int left = next(s, -1);
int right = prev(s, n);
for (int i = next(s, -1), j = prev(s, n); i < j; i = next(s, i), j = prev(s, j)) {
if (!equals(s[i], s[j]))
return false;
}
return true;
}
private:
int next(const string &s, int i) {
i++;
while (!isalnum(s[i])) i++;
return i;
}
int prev(const string &s, int j) {
j--;
while (!isalnum(s[j])) j--;
return j;
}
bool equals(char a, char b) {
a = tolower(a);
b = tolower(b);
return a == b;
}
};
int main(int argc, char **argv)
{
Solution solution;
string s1 = "A man, a plan, a canal: Panama";
string s2 = "race a car";
string s3 = "1a2";
cout << solution.isPalindrome(s1) << endl;
cout << solution.isPalindrome(s2) << endl;
cout << solution.isPalindrome(s3) << endl;
return 0;
}