-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path125.cpp
More file actions
26 lines (26 loc) · 666 Bytes
/
125.cpp
File metadata and controls
26 lines (26 loc) · 666 Bytes
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
// Author: btjanaka (Bryon Tjanaka)
// Problem: (LeetCode) 125
// Title: Valid Palindrome
// Link: https://leetcode.com/problems/valid-palindrome/
// Idea: Check if letters on left and right are equal, ignoring non-alphanumeric
// characters.
// Difficulty: easy
// Tags: string, implementation
class Solution {
public:
bool isPalindrome(string s) {
int left = 0, right = s.size() - 1;
while (left < right) {
if (!isalnum(s[left]))
++left;
else if (!isalnum(s[right]))
--right;
else {
if (tolower(s[left]) != tolower(s[right])) return false;
++left;
--right;
}
}
return true;
}
};