-
Notifications
You must be signed in to change notification settings - Fork 1
/
125.valid-palindrome.cpp
47 lines (43 loc) · 1.11 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
/*
* @lc app=leetcode id=125 lang=cpp
*
* [125] Valid Palindrome
*/
// @lc code=start
#include<string>
using namespace std;
class Solution {
public:
bool isPalindrome(string s) {
/*
* Time complexity : O(∣s∣).
* Space complexity : O(1).
* Runtime: 8 ms, faster than 99.31% of C++ online submissions for Valid Palindrome.
* Memory Usage: 7.3 MB, less than 69.13% of C++ online submissions for Valid Palindrome.
*/
int n = s.size();
int left = 0, right = n -1;
while (left < right)
{
while (left < right && !isalnum(s[left]))
{
++left;
}
while(left < right && !isalnum(s[right]))
{
--right;
}
if (left < right)
{
if (tolower(s[left]) != tolower(s[right]))
{
return false;
}
++left;
--right;
}
}
return true;
}
};
// @lc code=end