Validate Palindrome (Easy)
Good evening! Here's our prompt for today.
You may see this problem at Microsoft, Amazon, Linkedin, Stripe, Paypal, Yelp, Airbnb, Reddit, Datadog, Rubrik, Gusto, Lucid Software, Digitalocean, Servicenow, Zoom, Intel, and Zoominfo.
Given a string str
, can you write a method that will return True
if is a palindrome and False
if it is not? If you'll recall, a palindrome
is defined as "a word, phrase, or sequence that reads the same backward as forward". For now, assume that we will not have input strings that contain special characters or spaces, so the following examples hold:
1std::string str = "thisisnotapalindrome";
2bool isPalindrome = isPalindrome(str);
3// false
4
5str = "racecar";
6isPalindrome = isPalindrome(str);
7// true
For an extra challenge, try to ignore non-alphanumerical characters. The final solution that we present will handle all edge cases.
Constraints
- Length of the given string <=
100000
- The string will consist of ASCII characters (some or all)
- Expected time complexity :
O(n)
- Expected space complexity :
O(1)
xxxxxxxxxx
29
bool isPalindrome(std::string str) {
// fill in
// with solution
return false;
}
// print your findings using this function!
void log() {
std::cout << isPalindrome("A Santa Lived As a Devil At NASA");
}
int main(int argc, char** argv) {
log();
// LOG
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
OUTPUT
Results will appear here.