I recently started leetcode and ive been getting better. I attempted a problem and i got through all 479/485 test cases. But my code is struggling with a large set of data.
I tried completing the code on my own, and it works good on small sets. Can somebody explain to me how I can make it more efficient with large sets of data, and what makes mine struggle.
Here is the Question: 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.
Here is my code:
class Solution {
public:
bool isPalindrome(string s) {
string noSpaceString = "";
for(int i = 0; i < s.length(); i++)
{
s[i] = tolower(s[i]);
if((int(s[i]) >= 97 && int(s[i]) <= 122) || (int(s[i]) >= 48 && int(s[i] <= 57)))
{
noSpaceString = noSpaceString + s[i];
}
}
string reverseStr = noSpaceString;
reverse(reverseStr.begin(), reverseStr.end());
if(noSpaceString == reverseStr)
return true;
else
return false;
}
};