Problem · String

Minimum Number of Non-Empty Disjoint Segments

Learn this problem
MediumIBMINTERNOA
See IBM hiring insights

Problem statement

Given a string, find the minimum number of non-empty disjoint segments a string can be partitioned into, such that each segment has no repeated characters. Choose the partitioning that yields the fewest such segments among all possible ways to partition the string.

Note: Each character of the string should be in exactly one segment.

Source correction (July 2, 2026) 𐔌՞ ܸ.ˬ.ܸ՞𐦯 : I updated this page to match the official source image from July 1, 2026. Example 1 now uses s = "abcc", the partition table is included, the old extra visible example that was not in this source was removed, the July 1 sighting date was added, and the official source image is shown below.

Function

minimumDisjointSegments(s: String) → int

Examples

Example 1

s = "abcc"return = 2

Possible partitions of the string:

Possible partitions of the string
String partitionNumber of Segments
"a", "b", "c", "c"4
"abc", "c"2
"ab", "c", "c"3

It can be concluded that the fewest number of partitioned segments is 2.

Example 2

s = "abdaa"return = 3

It can be concluded that the partition "abd", "a", and "a" is the optimal partitioning of s into non-empty disjoint segments, each having no repeating characters.

Constraints

  • 1 <= |s| <= 2 * 10^5
  • It is guaranteed that the string s consists only of lowercase English letters.

More IBM problems

drafts saved locally
public int minimumDisjointSegments(String s) {
  // write your code here
}
s"abcc"
expected2
checking account