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:
1String str = "thisisnotapalindrome";
2boolean 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
60
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import static org.junit.Assert.*;
// solution
class Solution {
public static boolean isPalindrome(String str) {
// fill in
// with solution
return false;
}
private static boolean isAlphaNumeric(char c) {
return false;
}
// print your findings using this function!
public static void log() {
System.out.println(isPalindrome("A Santa Lived As a Devil At NASA"));
}
}
OUTPUT
Results will appear here.