forked from KnowledgeCenterYoutube/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
125_valid_palindrome
55 lines (48 loc) · 1.47 KB
/
125_valid_palindrome
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
Leetcode 125: Valid Palindrome
Detailed video explanation: https://youtu.be/028pTf2kBFI
=========================================================
C++:
----
class Solution {
public:
bool isPalindrome(string s) {
if(s.length() <= 1) return true;
int st = 0, en = s.length()-1;
while(st < en){
while(st < en && !isalnum(s[st])) st++;
while(st < en && !isalnum(s[en])) en--;
if(st < en && tolower(s[st]) != tolower(s[en])) return false;
st++;
en--;
}
return true;
}
};
Java:
----
class Solution {
public boolean isPalindrome(String s) {
if(s.length() <= 1) return true;
int st = 0, en = s.length()-1;
while(st < en){
while(st < en && !Character.isLetterOrDigit(s.charAt(st))) st++;
while(st < en && !Character.isLetterOrDigit(s.charAt(en))) en--;
if(st < en && Character.toLowerCase(s.charAt(st)) != Character.toLowerCase(s.charAt(en))) return false;
st++;
en--;
}
return true;
}
}
Python3:
--------
class Solution:
def isPalindrome(self, s: str) -> bool:
if len(s) <= 1: return True
st, en = 0, len(s)-1
while st < en:
while st < en and not s[st].isalnum(): st += 1
while st < en and not s[en].isalnum(): en -= 1
if st < en and s[st].lower() != s[en].lower(): return False
st, en = st+1, en-1
return True