-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode-125-Valid_Palindrome.cpp
52 lines (39 loc) · 1.22 KB
/
leetcode-125-Valid_Palindrome.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
/* -- Id = 125 -- */
#include <cstdlib>
#include <iostream>
#include <string>
#include <cctype>
//#include <algorithm>
using std::string;
bool isPalindrome(string s) {
for(size_t begin = 0, end = static_cast<size_t>(s.size()-1);
begin != end && (begin < s.size()-1 && end > 0);
++begin, --end)
{
while (!std::isalpha(s[begin]) && !std::isdigit(s[begin]) && begin < s.size()-1)
++begin;
while (!std::isalpha(s[end]) && !std::isdigit(s[end]) && end > 0)
--end;
if (end <= begin)
break;
if (((std::isalpha(s[begin]) || std::isdigit(s[begin])) &&
(std::isalpha(s[end])) || std::isdigit(s[end])) &&
(std::tolower(s[begin]) != std::tolower(s[end])))
{
return false;
}
}
return true;
}
//string temp_s = s;
// temp_s.erase(std::remove_if(temp_s.begin(), temp_s.end(), [](char c) {
// return !std::isalpha(c);
// }), temp_s.end());
// for (auto &i : temp_s)
// i = std::tolower(i);
// std::cout << temp_s <<std::endl;
int main(int argc, char const *argv[])
{
std::cout << isPalindrome("ab2a") << std::endl;
return EXIT_SUCCESS;
}