Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    The fewest cuts into palindromes

    HardAbout 40 minFull AccessLearn it: Palindromes in C#: Longest Palindromic Substring, Counting Palindromes and Fewest Cuts

    The task

    A label printer can only print strings that read the same forwards and backwards, so a long label is cut into such pieces, and every cut costs time.

    Complete MinPalindromeCuts(string s). Return the fewest cuts that split s into pieces that are all palindromes (they read the same in both directions; matching is case-sensitive). An empty string or a string that is already a palindrome needs 0 cuts. The string is never null.

    Signature

    public static int MinPalindromeCuts(string s)

    Examples

    • "xxy" → 1. "xx" | "y".
    • "abaab" → 1. "a" | "baab"; cutting off the longer "aba" first needs 2 cuts.

    Limits: Up to 2,000 characters.

    The tests check: an empty string, one letter, two different letters, a whole palindrome, every letter alone, three palindromes in a row, the longest first piece being the wrong choice, and a 2,000-letter text in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // The fewest cuts that split s into pieces that are all palindromes.
          public static int MinPalindromeCuts(string s)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.