Skip to the code
cf.
+10 XP

#99 Flip Only the Letters

easy

A text effect in a slide editor reverses the order of the letters in a label but leaves every other character (digits, spaces, punctuation) exactly where it was.

Write flipLetters(label) that returns the new label. A letter is any of a-z or A-Z; letters keep their case.

Example 1

Input: label = "ab-cd"
Output: "dc-ba"

The letters abcd are reversed to dcba, and the dash stays at index 2.

Example 2

Input: label = "v2-Beta!"
Output: "a2-teBv!"

The digit, dash, and ! stay in place; the letters vBeta become ateBv.

Constraints

  • 0 <= label.length <= 100000
  • label contains printable ASCII characters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).