Problem overview
The method isPalindrome receives a String named input and returns a Boolean: true if the text reads the same backward as forward, false otherwise. Every character counts, including spaces, so "madam racecar madam" is a palindrome (its reverse is the same string) and "abcbx" is not.
Approach: reverse, then compare
A string is a palindrome exactly when it equals its own reverse. That turns the problem into two operations: build the reversed string, then compare it with the original. Scala has both built in: reverse on a string returns a new reversed string, and == compares two strings by their contents.
This is the shortest solution and the easiest to see is correct. It has a cost: the reversed copy takes O(n) extra space, where n is the length of the string. The task statement asks for O(1) extra space, which a two-pointer check meets. That approach compares the first and last characters, then the second and second-to-last, moving inward until the pointers meet, and never builds a copy. This lesson focuses on the version that leans on the standard library.
Step by step
- Reverse the input. Store
input.reversein avalnamedreversedInput. The call takes no parentheses and returns a new string. The original is untouched, because strings can't be modified. - Compare and return. Make
input == reversedInputthe last line of the method. Scala returns the value of the last expression, so noreturnis needed, and since a comparison is already aBoolean, there is no need for anifthat returnstrueorfalseby hand.
The solution in Scala
class Solution {
def isPalindrome(input: String): Boolean = {
val reversedInput = input.reverse
input == reversedInput
}
}
The Scala details worth knowing:
==compares contents. In Scala,==on strings checks whether they hold the same characters, even if they are two separate objects. That differs from Java, where==on objects compares references. For a reference comparison, Scala haseq.- The last expression is the result. The block ends with
input == reversedInput, with no semicolon and noreturn. If you added a statement after it, the method would return that instead. reversecomes from Scala's collection methods. AStringbehaves like a sequence of characters, andreversegives back aString.- Case and spaces matter. The comparison is exact:
"Aba"is not a palindrome, because'A'and'a'are different characters, and a phrase like"A man a plan"is judged with its spaces included.
Complexity
Time: O(n). Reversing visits each character once, and the comparison looks at up to n characters. Both are linear, so the total is O(n).
Space: O(n). The reversed string is a second copy of the input. Because of that copy, this solution does not reach the O(1) extra space of the two-pointer check.
A two-pointer version
For comparison, this version meets the O(1) space target:
def isPalindrome(input: String): Boolean = {
var i = 0
var j = input.length - 1
while (i < j) {
if (input(i) != input(j)) return false
i += 1
j -= 1
}
true
}
It stops as soon as one pair of characters differs, so it can finish early, and it allocates no new string. When i reaches j, every pair matched.
Edge cases and common mistakes
- Empty string. It equals its own reverse, so the result is
true. - One character. Always a palindrome.
- Two different characters.
"ab"reverses to"ba", so the result isfalse. - Even and odd lengths.
"abba"and"abcba"both work with no special handling, since the whole string is compared. - Upper and lower case.
"Aba"returnsfalse. To ignore case, convert first withtoLowerCase, but only if the task says to. - Calling
reverse()with parentheses. This does not compile for the string method, so writeinput.reverse.
Testing
"madam racecar madam"givestrue."abcbx"givesfalse.""givestrue."ab"givesfalse, and"aa"givestrue."abcba"givestrue.
