Skip to the code
cf.
+50 XP

#549 Fewest Palindrome Cuts in a Label

hard

A puzzle app cuts a label s into pieces so that every piece reads the same forward and backward (a palindrome). Single letters are palindromes.

Write fewestCuts(s) that returns the fewest cuts needed so that every piece is a palindrome. A label that is already a palindrome needs 0 cuts.

Example 1

Input: s = "noonabba"
Output: 1

noon | abba.

Example 2

Input: s = "abc"
Output: 2

No two neighboring letters match, so every letter is its own piece.

Constraints

  • 1 <= s.length <= 2000
  • s contains only lowercase English letters.
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).