isPalindrome
*-----------------------------------------------------------------
Challenge: 11-isPalindrome
Difficulty: Intermediate
Prompt:
- Write a function called isPalindrome that accepts a single string argument, then returns true or false depending upon whether or not the string is a palindrome.
- A palindrome is a word or phrase that are the same forward or backward.
- Casing and spaces are not included when considering whether or not a string is a palindrome.
- If the length of the string is 0 or 1, return true.
Examples:
isPalindrome('SEI Rocks'); //=> false
isPalindrome('rotor'); //=> true
isPalindrome('A nut for a jar of tuna'); //=> true
isPalindrome(''); //=> true
-----------------------------------------------------------------\*/
// Your solution for 11-isPalindrome here:
WATCH VIDEO HERE:
Written Solution
const isPalindrome = (str) => {
let lowerCaseStr = str.toLowerCase();
let noSpaceLowerCaseStr = lowerCaseStr.split(' ').join('');
if (noSpaceLowerCaseStr.length === 0 || noSpaceLowerCaseStr.length === 1) {
return true;
}
// if string is same as reversed string > return True
let reversedStr = noSpaceLowerCaseStr.split('').reverse().join('');
if (noSpaceLowerCaseStr === reversedStr) {
return true;
}
return false;
};
def isPalindrome(str):
lowerCaseStr = str.lower()
# trimmedStr = lowerCaseStr.strip()
noSpaceLowerCaseStr = lowerCaseStr.replace(" ","")
if len(noSpaceLowerCaseStr) == 0 or len(noSpaceLowerCaseStr) == 1:
return True
# check if string is same as reverse string > true
reversedStr = noSpaceLowerCaseStr[::-1]
if noSpaceLowerCaseStr == reversedStr:
return True
return False
this solution is NOT
the most efficient but it aids you in thinking systematically.
most efficient meaning in Time or Space complexity. You can read up more about this!
instead, i have attempted it as though i were in an interview and not knowing certain methods thus searching for it. it's normal not to know things so don't feel shy to ask your interviewer if you can use Google or StackOverflow. Preferably not Chatgpt :P la but if they can't see you screen you can try??? LOL