All problems

Problem 530

Optimal Partition of String

Medium · 20 pts · String, Greedy, Hash Table

Problem

Split a string into the fewest consecutive pieces such that no letter appears more than once inside any single piece. Every character must belong to exactly one piece. A greedy scan is optimal: keep adding characters to the current piece, and start a new piece only when the next character is already in the current one.

Input

A single string S of lowercase English letters.

Output

Print the minimum number of pieces.

Constraints

1 <= |S| <= 100000

Example input

abacaba

Example output

4

Explanation

abacaba can be split as ab | ac | ab | a, where no piece repeats a letter. Three pieces are impossible because each piece has at most one a and there are four a's, so the answer is 4.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Longest Word in Dictionary through Deleting Next: Minimum Window Subsequence