Ctrl K

Most Distinct Pieces

Medium
You are given a string text of lowercase English letters. Cut it into non-empty consecutive pieces (gluing the pieces back in order gives the original string). No two pieces may be equal. Return the largest possible number of pieces.
Example 1
text="xyxyzz" → 4

For instance, x | y | xy | z | z fails because z appears twice, while x | y | xy | zz works with 4 different pieces. Five is not achievable.

Example 2
text="bbb" → 2

Only one piece can be b, so the best is b | bb.

Example 3
text="q" → 1
Constraints
  • 1 ≤ len(text) ≤ 16
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsKeypad Words MediumPalindromes From a Set of Letters MediumDoes the String Fit the Letter Template MediumNext Time From the Same Digits MediumCount the Tile Sequences Medium
loading Python…
Input
text="xyxyzz"
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 75 tests on the server, hidden ones included.