Most Disjoint Character-Closed Substrings with Minimum Total Length

Quick Overview

Most Disjoint Character-Closed Substrings with Minimum Total Length. Choose nonempty,non-overlapping substrings of s.

Most Disjoint Character-Closed Substrings with Minimum Total Length

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Choose nonempty,non-overlapping substrings of s. If a chosen substring contains a character,it must contain every occurrence of that character in the entire string. First maximize the number chosen; among those solutions,minimize their total length. The source guarantees a unique minimum-length collection. Return its substring strings in any order. ```cpp class Solution { public: vector<string> maxNumOfSubstrings(string s); }; ``` ### Examples **Example 1:** ```text Input: s = "abac" Output: ["b","c"] Explanation: These closed single-character substrings give count2 and total length2. Replacing b with aba keeps count2 but increases total length. ``` **Example 2:** ```text Input: s = "zzxyx" Output: ["zz","y"] Explanation: Both z occurrences must stay together. Choosing y instead of xyx gives the same count2 with smaller total length3. ``` ### Constraints - s has length 1 to 100000. - s contains only lowercase English letters. - Among maximum-count collections,the minimum-total-length collection is guaranteed unique; output order is unrestricted. ### Contract considerations The collection is unique under the length tie-break,but its returned order is unrestricted;[b,c] and[c,b] are both valid for abac. ```hint Read the boundary condition Enforce character coverage and non-overlap. Maximum count takes priority over total length, and output order remains unrestricted. ``` ### What a Strong Answer Covers The collection is unique under the length tie-break,but its returned order is unrestricted;[b,c] and[c,b] are both valid for abac. ### Follow-up Questions Optional practice extension,separate from the original task: How would the method change for a substantially larger character alphabet?

Overview: Most Disjoint Character-Closed Substrings with Minimum Total Length. Choose nonempty,non-overlapping substrings of s.

Google logo
Google
Aug 17, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Choose nonempty,non-overlapping substrings of s. If a chosen substring contains a character,it must contain every occurrence of that character in the entire string. First maximize the number chosen; among those solutions,minimize their total length. The source guarantees a unique minimum-length collection. Return its substring strings in any order.

class Solution {
public:
    vector<string> maxNumOfSubstrings(string s);
};

Examples

Example 1:

Input: s = "abac"
Output: ["b","c"]
Explanation: These closed single-character substrings give count2 and total length2. Replacing b with aba keeps count2 but increases total length.

Example 2:

Input: s = "zzxyx"
Output: ["zz","y"]
Explanation: Both z occurrences must stay together. Choosing y instead of xyx gives the same count2 with smaller total length3.

Constraints

  • s has length 1 to 100000.
  • s contains only lowercase English letters.
  • Among maximum-count collections,the minimum-total-length collection is guaranteed unique; output order is unrestricted.

Contract considerations

The collection is unique under the length tie-break,but its returned order is unrestricted;[b,c] and[c,b] are both valid for abac.

What a Strong Answer Covers Guidance

The collection is unique under the length tie-break,but its returned order is unrestricted;[b,c] and[c,b] are both valid for abac.

Follow-up Questions Guidance

Optional practice extension,separate from the original task: How would the method change for a substantially larger character alphabet?