Optimal Partition of String

Given a string `s`, split it into one or more **substrings** so that no letter appears more than once inside any single substring. In other words, the characters of each substring are all **unique**. Return *the **minimum** number of substrings in such a partition*. Every character of `s` must belong to exactly one substring.

30:00

Optimal Partition of String
medium
Free written solution
Topics
Companies

Given a string s, split it into one or more substrings so that no letter appears more than once inside any single substring. In other words, the characters of each substring are all unique.

Return the minimum number of substrings in such a partition.

Every character of s must belong to exactly one substring.

Example 1:
Input: {"s":"abacaba"}
Output: 4
Constraints:
  • 1≤s.length≤1051 \leq s.\text{length} \leq 10^5

  • s consists only of English lowercase letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"abacaba"}

Start with one open substring and an empty set of letters. Read "abacaba" left to right and only cut when a letter would repeat.

s, cut into substrings

0
a
1
b
2
a
3
c
4
a
5
b
6
a

letters in the current substring (substring 1)

a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

closed:

none yet

parts

1

Variables
VariableValue
i-
ch-
seen{}
parts1
DepthFunction Call
Stack empty
0/18