-
Notifications
You must be signed in to change notification settings - Fork 5
/
day-125.cpp
63 lines (46 loc) · 1.35 KB
/
day-125.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
/*
Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric
characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid
palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
Constraints:
s consists only of printable ASCII characters.
*/
// Solved using O(N) & O(1) approach, beats 89.40% cpp submissions
class Solution {
public:
bool isValidCharacter(char ch) {
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
(ch >= '0' && ch <= '9')) {
return true;
}
return false;
}
bool isCharacterEqual(char a, char b) {
char aLower = tolower(a);
char bLower = tolower(b);
if (aLower == bLower) return true;
return false;
}
bool isPalindrome(string s) {
int start = 0;
int end = s.length() - 1;
int N = end;
while (start < end) {
while ((start <= N) && !isValidCharacter(s[start])) start += 1;
while ((end >= 0) && !isValidCharacter(s[end])) end -= 1;
if (!(start <= N && end >= 0)) return true;
if (!isCharacterEqual(s[start], s[end])) return false;
start += 1;
end -= 1;
}
return true;
}
};