Ctrl K

Fewest Repeat-Free Pieces

Medium
You are given a string text of lowercase English letters. Cut it into consecutive pieces so that no letter appears twice within any piece. Every character of the string goes into exactly one piece. Return the smallest possible number of pieces.
Example 1
text="banana" → 3

For example ban + an + a: each piece has distinct letters. Two pieces are not enough: banana has three as and a piece can hold only one.

Example 2
text="queue" → 2

que + ue.

Example 3
text="dog" → 1

All letters differ, so one piece is enough.

Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPieces Without Shared Letters MediumNo Twin Letters Side by Side MediumSplit Letters Into Palindromes MediumDistinct Letter Counts MediumThe Tightest Shared Piece Medium
loading Python…
Input
text="banana"
Expected
3

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