-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode125_valid_palindrome.cpp
61 lines (49 loc) · 1.2 KB
/
leetcode125_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
53
54
55
56
57
58
59
60
61
/**
* @file leetcode58_length_of_last_word.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/05/31 22:16
* @brief https://leetcode.com/problems/valid-palindrome
*
**/
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class Solution {
public:
bool isPalindrome(string s) {
size_t len = s.length();
int b = 0;
int e = len - 1;
while (b <= e) {
if (!isalnum(s[b])) {
++b;
continue;
}
if (!isalnum(s[e])) {
--e;
continue;
}
if (tolower(s[b]) != tolower(s[e])) {
return false;
}
++b;
--e;
}
return true;
}
};
int main() {
std::string s;
while (1) {
std::cout << "Input string (empty string to exit): ";
std::getline(std::cin, s);
if (s.length() == 0) {
return 0;
}
Solution solution;
auto ret = solution.isPalindrome(s);
std::cout << (ret ? "true" : "false") << std::endl;
}
return 0;
}