-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValidPalindrome.cpp
More file actions
35 lines (27 loc) · 851 Bytes
/
ValidPalindrome.cpp
File metadata and controls
35 lines (27 loc) · 851 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
// https://leetcode.com/explore/interview/card/top-interview-questions-easy/127/strings/883/
#include <iostream>
#include <string>
bool isPalindrome(std::string s) {
int i = 0;
int j = s.size();
while (i < j) {
if (!std::isalnum(s[i])) {
i++;
} else if (!std::isalnum(s[j])) {
j--;
} else if (std::tolower(s[i++]) != std::tolower(s[j--])) {
return false;
}
}
return true;
}
int main()
{
std::string s = "A man, a plan, a canal: Panama";
std::cout << s << " : " << std::boolalpha << isPalindrome(s) << std::endl;
s = "race a car";
std::cout << s << " : " << std::boolalpha << isPalindrome(s) << std::endl;
s = "0P";
std::cout << s << " : " << std::boolalpha << isPalindrome(s) << std::endl;
return 0;
}