FunctionRacerProblemsRace

Neighbor Swaps to a Mirror

HardStringsPython, JavaScript, TypeScript, Java, C++

A mirror is a string that reads the same from both ends. The letters of text can be put in an order that is a mirror. One move swaps two letters that are next to each other.

Return the fewest moves that turn text into a mirror.

Example 1

text "abab"
returns 1

Swap the last two letters: abab becomes abba.

Example 2

text "abbac"
returns 2

Two swaps: abbac, abbca, abcba. The c has no partner, so it ends in the middle.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Strings problems