forked from wbhima/CodeforALL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Valid Palindrome.cpp
47 lines (38 loc) · 1.01 KB
/
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
/*
Leetcode: Valid Palindrome
Given a string s, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Example 1:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
Constraints:
1 <= s.length <= 2 * 105
s consists only of printable ASCII characters.
*/
class Solution {
public:
bool isPalindrome(string s) {
int i=0, j=s.length()-1;
while(i<j) {
if(!isalnum(s[i]) && !isalnum(s[j])) {
i++;
j--;
}
else if(!isalnum(s[i]))
i++;
else if(!isalnum(s[j]))
j--;
else if(tolower(s[i])!=tolower(s[j]))
return false;
else {
i++;
j--;
}
}
return true;
}
};