-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid_palindrome.cpp
More file actions
62 lines (59 loc) · 1.47 KB
/
valid_palindrome.cpp
File metadata and controls
62 lines (59 loc) · 1.47 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
/*
* =====================================================================================
*
* Filename: valid_palindrome.cpp
*
* Description: 125. Valid Palindrome.
* Given a string, determine if it is a palindrome, considering only
* alphanumeric characters and ignoring cases.
*
* Version: 1.0
* Created: 04/08/19 11:29:07
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
class Solution
{
public:
bool isPalindrome(const std::string &s)
{
int i = 0;
int j = s.size() - 1;
while (i < j)
{
if (!isalnum(s.at(i)))
{
i++;
continue;
}
if (!isalnum(s.at(j)))
{
j--;
continue;
}
if (toupper(s.at(i)) != toupper(s.at(j)))
{
return false;
}
i++;
j--;
}
return true;
}
};
int main(int argc, char* argv[])
{
std::string s = "A man, a plan, a canal: Panama";
auto is_pal = Solution().isPalindrome(s);
printf("Input: `%s`, is palindrome? %s\n", s.c_str(), (is_pal ? "Yes" : "No"));
return 0;
}