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.
Solution:
string removeNonAlphabetsAndConvertToUpperCase(string s)
{
for(int i=0; i<s.size(); i++)
{
if((s[i] < 'A' || s[i] > 'Z') && (s[i] < 'a' || s[i] > 'z') && (s[i] < '0' || s[i] > '9'))
{
s.erase(i,1);
i--;
}
}
transform(s.begin(), s.end(), s.begin(), ::toupper);
return s;
}
string getReverse(string s)
{
string s1;
for(int i = s.length()-1; i>=0; i--)
s1.push_back(s.at(i));
return s1;
}
bool isPalindrome(string s) {
string s2 = removeNonAlphabetsAndConvertToUpperCase(s);
string s1 = getReverse(s2);
if(s1.compare(s2) == 0)
return true;
return false;
}
Complete Code:
// function to check if it is palindrome or not
#include <bits/stdc++.h>
using namespace std;
string removeNonAlphabetsAndConvertToUpperCase(string s)
{
for(int i=0; i<s.size(); i++)
{
if((s[i] < 'A' || s[i] > 'Z') && (s[i] < 'a' || s[i] > 'z') && (s[i] < '0' || s[i] > '9'))
{
s.erase(i,1);
i--;
}
}
transform(s.begin(), s.end(), s.begin(), ::toupper);
return s;
}
string getReverse(string s)
{
string s1;
for(int i = s.length()-1; i>=0; i--)
s1.push_back(s.at(i));
return s1;
}
bool isPelendrome(string s) {
string s2 = removeNonAlphabetsAndConvertToUpperCase(s);
string s1 = getReverse(s2);
cout << s1 << endl << s2 << endl;
if(s1.compare(s2) == 0)
return true;
return false;
}
int main()
{
string s = "0p";
cout << isPelendrome(s) << endl;
return 0;
}
You completed a number of nice points there. I did a search on the subject matter and found nearly all folks will go along with with your blog.
I was pretty pleased to discover this great site. I want to to thank you for ones time due to this fantastic read!! I definitely appreciated every part of it and I have you book marked to look at new stuff on your web site.
I was very happy to discover this great site. I need to to thank you for your time for this particularly wonderful read!! I definitely liked every part of it and I have you bookmarked to check out new information on your blog.
Good day! I simply want to offer you a big thumbs up for the excellent info youve got right here on this post. Ill be coming back to your site for more soon.
905345 882886You made some decent factors there. I looked on the internet for the problem and found most individuals will go along with with your website. 499330
Itís hard to find knowledgeable people for this topic, but you seem like you know what youíre talking about! Thanks
You ought to take part in a contest for one of the best websites on the net. I am going to recommend this website!
Itís nearly impossible to find well-informed people for this subject, however, you seem like you know what youíre talking about! Thanks