-
Notifications
You must be signed in to change notification settings - Fork 0
/
Valid Palindrome.txt
140 lines (101 loc) · 2.71 KB
/
Valid Palindrome.txt
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
Example 1:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
Example 3:
Input: s = " "
Output: true
Explanation: s is an empty string "" after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.
Constraints:
1 <= s.length <= 2 * 105
s consists only of printable ASCII characters.
Solution:
class Solution
{
public:
bool isPalindrome(string s)
{
string c = "";
char a;
for (int i = 0; i < s.size(); i++)
{
a = s[i];
if ((int)a > 47 && (int)a < 58)
c += a;
else if ((int)a > 96 && (int)a < 123)
c += a;
else if ((int)a > 64 && (int)a < 91)
c += char((int)a + 32); // conversion of capital letter into small letter
}
return ispalindrome(c);
}
bool ispalindrome(string s)
{
int l = 0, h = s.size() - 1;
while (l < h)
{
if (s[l++] != s[h--])
return false;
}
return true;
}
};
#include<string>
#include<iostream>
#include<vector>
using namespace std;
class Solution {
private:
bool valid(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 a) {
int s = 0;
int e = a.length()-1;
while(s<=e) {
if(a[s] != a[e])
{
return 0;
}
else{
s++;
e--;
}
}
return 1;
}
public:
bool isPalindrome(string s) {
//faltu character hatado
string temp = "";
for(int j=0; j<s.length(); j++) {
if(valid(s[j])) {
temp.push_back(s[j]);
}
}
//lowercase me kardo
for(int j=0; j<temp.length(); j++) {
temp[j] = toLowerCase(temp[j]);
}
//check palindrome
return checkPalindrome(temp);
}
};