-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvalid_palindrome.py
48 lines (41 loc) · 1.04 KB
/
valid_palindrome.py
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
class Solution:
def isPalindrome(self, s: str) -> bool:
s = s.lower()
stt = []
for st in s:
if st >= 'a' and st <= 'z' or st >= '0' and st <= '9':
stt.append(st)
print(stt)
i = 0; j = len(stt) - 1
while(i <= j):
if stt[i] != stt[j]:
return False
i += 1
j -= 1
return True
'''
CPP solution -
class Solution {
public:
bool isPalindrome(string s) {
if(s.length() == 0) return true;
int m=s.length()-1;
int i=0;
while(i<m) {
if(isalnum(s[i]) && isalnum(s[m])) {
if(tolower(s[i]) != tolower(s[m]))
return false;
else{
i++, m--;
continue;
}
}
else {
if(!isalnum(s[i])) i++;
if(!isalnum(s[m])) m--;
}
}
return true;
}
};
'''