String PalindromeLoading saved progress…

String Palindrome

A palindrome is a string that reads the same forwards and backwards: "racecar", "level", "abba". Implement stringPalindrome(str) — return true if the input reads the same in both directions, false otherwise.

Treat the comparison as strict: case matters and every character counts. "Racecar" is not a palindrome (capital R differs from lowercase r), and "race car" is not a palindrome (the space at index 4 differs from the e at the mirror position). If the caller wants to normalize, they can do it before calling you.

Signature

function stringPalindrome(str) {
  // returns true if str reads the same forwards and backwards,
  // false otherwise.
}

Examples

stringPalindrome('racecar'); // true
stringPalindrome('level');   // true
stringPalindrome('hello');   // false
stringPalindrome('');           // true  — empty string is trivially a palindrome
stringPalindrome('a');          // true  — single char mirrors itself
stringPalindrome('Racecar');    // false — case-sensitive
stringPalindrome('race car');   // false — whitespace is significant

Notes

  • Empty string returns true. There is nothing to mismatch.
  • Single character returns true. It is its own mirror.
  • Case-sensitive'A' !== 'a'. Do not lowercase the input.
  • Whitespace is significant' ' is just another character.
  • Don't mutate or return the input string. Return a boolean.
Loading editor…