-
Notifications
You must be signed in to change notification settings - Fork 17
/
validPalindromeleetcode.cpp
49 lines (43 loc) · 1.15 KB
/
validPalindromeleetcode.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
class Solution {
private: bool isValid(char ch){
if((ch >= 'a'&& ch <= 'z') || (ch>='A' && ch <= 'Z') || (ch >= '0'&& ch <= '9')){
return 1;
}
return 0;
}
char toLowerCase(char ch){
if((ch >='a' && ch<= 'z') || (ch >='0' && ch<= '9')){
return ch;
}else{
char temp = ch -'A' + 'a';
return temp;
}
}
bool checkPalindrome(string s){
int start = 0;
int end = s.length()-1;
while(start<=end){
if(s[start] != s[end]){
return 0;
}else{
start++;
end--;
}
}
return 1;
}
public:
bool isPalindrome(string s) {
string temp = "";
for(int i = 0 ; i< s.length() ; i++){
if(isValid(s[i])){
temp.push_back(s[i]);
}
}
//lowercase
for(int j =0 ; j< temp.length() ; j++){
temp[j] = toLowerCase(temp[j]);
}
return checkPalindrome(temp);
}
};