forked from cpselvis/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution125.cpp
More file actions
59 lines (54 loc) · 970 Bytes
/
solution125.cpp
File metadata and controls
59 lines (54 loc) · 970 Bytes
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
/**
* Valid Palindrome
*
* cpselvis([email protected])
* September 13th, 2016
*/
#include<iostream>
using namespace std;
class Solution {
public:
bool isPalindrome(string s) {
if (s.size() == 0)
{
return true;
}
int i = 0, j = s.size() - 1;
while (i <= j)
{
if (!isLetterOrDigit(s[i]))
{
i ++;
}
else if (!isLetterOrDigit(s[j]))
{
j --;
}
else
{
if (tolower(s[i]) != tolower(s[j]))
{
return false;
}
i ++;
j --;
}
}
return true;
}
bool isLetterOrDigit(char ch)
{
if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
return true;
}
return false;
}
};
int main(int argc, char **argv)
{
Solution s;
cout << s.isPalindrome("A man, a plan, a canal: Panama") << endl;
cout << s.isPalindrome("race a car") << endl;
cout << s.isPalindrome(".,") << endl;
}