Problem overview
The function isPalindrome receives a string, input, and returns true when it reads the same forwards and backwards, and false otherwise. Spaces count as characters like any other. In the first example, "madam racecar madam" reversed is again "madam racecar madam", so the answer is true. For "abcbx" the reverse is "xbcba", which differs, so the answer is false.
The statement's note asks for O(n) time and O(1) extra space, and its hints describe two pointers that walk toward the middle. This lesson solves the same problem a shorter way and trades the space guarantee for simplicity.
Approach: reverse, then compare
The definition of a palindrome is already an algorithm: build the reversed string and check whether it equals the original. Kotlin gives you both halves ready-made. reversed() produces the reversed text, and == compares two strings by content.
The two-pointer method from the statement's hints checks pairs from the outside in, starting with input[0] against input[n - 1], and uses no extra memory. This lesson's version allocates a second string the same length as the input, so its space use is O(n), not O(1). The time is still linear, and the code is two lines. Once this makes sense, the two-pointer solution is the natural next step.
Step by step
- Reverse the input.
input.reversed()returns a new string with the characters in the opposite order and stores it in thevalnamedreversedInput. The originalinputdoesn't change, because strings in Kotlin never change in place. - Compare and return.
input == reversedInputis already aBoolean,trueexactly when the two strings hold the same characters in the same order. Returning it directly replaces anifthat would returntrueorfalse.
The solution in Kotlin
class Solution {
fun isPalindrome(input: String): Boolean {
val reversedInput = input.reversed()
return input == reversedInput
}
}
Kotlin details worth knowing:
==compares contents. In Kotlin,==callsequals, so two different string objects with the same characters are equal. The operator===compares references and would givefalsehere for a palindrome that was built separately. Always use==for strings.reversed()on aStringreturns aString. The same name exists on lists, where it also returns a new list. The in-place version for a mutable list isreverse(), without thed.- Case counts. The comparison is case-sensitive, so
"Aba"is not a palindrome: its reverse is"abA". - Surrogate pairs.
String.reversed()keeps a surrogate pair, such as an emoji, in the right order, so emoji strings are handled correctly. - Inline form. The function could also be written as
fun isPalindrome(input: String): Boolean = input == input.reversed(). The two-line version is kept here so each step has a name.
Complexity
Time: O(n). Reversing visits each of the n characters once, and the comparison looks at up to n characters, stopping at the first difference.
Space: O(n). reversed() allocates a second string of the same length. The two-pointer approach in the statement's hints reaches O(1) because it compares characters in place, using only two indexes.
Edge cases and common mistakes
- Empty string.
""reversed is"", so the function returnstrue. An empty sequence reads the same both ways. - Single character. Any one-character string is a palindrome.
- Spaces are characters.
"race car"reversed is"rac ecar", so it is not a palindrome here, even though the letters alone are. Skipping spaces or ignoring case would be a different problem. - Uppercase and lowercase differ.
"Aba"returnsfalse, as explained above. - Using
===. It compares references and gives unreliable answers for strings. Use==. - Expecting the input to change.
input.reversed()doesn't modifyinput. Nothing is reversed unless you keep the returned value.
Testing
"madam racecar madam"givestrue."abcbx"givesfalse.""givestrue."a"givestrue."abba"givestrue, and"abca"givesfalse."Aba"givesfalse.