← Back to challenges

Minimum Steps to a Palindrome

PythonHardstringsalgorithms

Instructions

Given an incomplete palindrome as a string, return the minimum letters needed to be added on to the end to make the string a palindrome.

Examples

min_palindrome_steps("race") ➞ 3
# Add 3 letters: "car" to make "racecar"

min_palindrome_steps("mada") ➞ 1
# Add 1 letter: "m" to make "madam"

min_palindrome_steps("mirror") ➞ 3
# Add 3 letters: "rim" to make "mirrorrim"

Notes

Trivially, words which are already palindromes should return 0.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.