-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution125.cpp
More file actions
41 lines (37 loc) · 827 Bytes
/
Solution125.cpp
File metadata and controls
41 lines (37 loc) · 827 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
//
// Solution125.cpp
// Algorithm
//
// Created by Pancf on 2020/12/13.
// Copyright © 2020 Pancf. All rights reserved.
//
#include "Solution125.hpp"
bool isASCIIChar(char ch)
{
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
}
bool isAlphaOrChar(char ch)
{
return isASCIIChar(ch) || (ch >= '0' && ch <= '9');
}
bool Solution125::isPalindrome(std::string s)
{
int i = 0, j = (int)s.size() - 1;
while (i < j) {
if (!isAlphaOrChar(s[i])) {
i++;
continue;
}
if (!isAlphaOrChar(s[j])) {
j--;
continue;
}
if (s[i] == s[j] || (isASCIIChar(s[i]) && isASCIIChar(s[j]) && abs(s[i]-s[j]) == 32)) {
i++;
j--;
} else {
return false;
}
}
return true;
}