-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path125. Valid Palindrome.cpp
64 lines (38 loc) · 1.06 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
// Using alnum
class Solution {
public:
bool isPalindrome(string s) {
if (s.empty())
return true;
int i = 0;
int j = s.length()-1;
while(i<=j) {
while (!isalnum(s[i])) i++;
while (!isalnum(s[j])) j--;
if (i<=j && tolower(s[i++]) != tolower(s[j--]))
return false;
}
return true;
}
};
--------
class Solution {
public:
bool isPalindrome(string s) {
if (s.empty())
return true;
int i = 0;
int j = s.length()-1;
while(i<=j) {
while (!((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= '0' && s[i] <= '9')))
i++;
while (!((s[j] >= 'A' && s[j] <= 'Z') || (s[j] >= 'a' && s[j] <= 'z') || (s[j] >= '0' && s[j] <= '9')))
j--;
if (i<=j && tolower(s[i++]) != tolower(s[j--])) {
// cout << i << " " << j << endl;
return false;
}
}
return true;
}
};