Taro Logo

Maximum Number of Non-Overlapping Substrings

Hard
Asked by:
Profile picture
4 views
Topics:
StringsGreedy Algorithms

Given a string s of lowercase letters, you need to find the maximum number of non-empty substrings of s that meet the following conditions:

  1. The substrings do not overlap, that is for any two substrings s[i..j] and s[x..y], either j < x or i > y is true.
  2. A substring that contains a certain character c must also contain all occurrences of c.

Find the maximum number of substrings that meet the above conditions. If there are multiple solutions with the same number of substrings, return the one with minimum total length. It can be shown that there exists a unique solution of minimum total length.

Notice that you can return the substrings in any order.

Example 1:

Input: s = "adefaddaccc"
Output: ["e","f","ccc"]
Explanation: The following are all the possible substrings that meet the conditions:
[
  "adefaddaccc"
  "adefadda",
  "ef",
  "e",
  "f",
  "ccc",
]
If we choose the first string, we cannot choose anything else and we'd get only 1. If we choose "adefadda", we are left with "ccc" which is the only one that doesn't overlap, thus obtaining 2 substrings. Notice also, that it's not optimal to choose "ef" since it can be split into two. Therefore, the optimal way is to choose ["e","f","ccc"] which gives us 3 substrings. No other solution of the same number of substrings exist.

Example 2:

Input: s = "abbaccd"
Output: ["d","bb","cc"]
Explanation: Notice that while the set of substrings ["d","abba","cc"] also has length 3, it's considered incorrect since it has larger total length.

Constraints:

  • 1 <= s.length <= 105
  • s contains only lowercase English letters.

Solution


Clarifying Questions

When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:

  1. What is the maximum length of the input string 's'?
  2. If no non-overlapping substrings can be found, what should the function return: an empty list, or null, or something else?
  3. If there are multiple valid sets of non-overlapping substrings that maximize the count, can I return any one of them, or is there a specific preference (e.g., shortest combined length, lexicographically smallest)?
  4. Can the input string 's' contain characters other than lowercase English letters, and should I expect an empty string as input?
  5. Is a substring defined as a contiguous sequence of characters, and should the resulting substrings be sorted in any particular order (e.g., by starting index or length)?

Brute Force Solution

Approach

The brute force method for finding the maximum number of non-overlapping substrings is all about trying every single combination. We essentially explore all possible ways to cut the given string into smaller pieces and then see which combination gives us the most valid substrings.

Here's how the algorithm would work step-by-step:

  1. Consider every possible substring that can be formed from the input string. Think of it like cutting the string at different places to create different segments.
  2. For each substring, check if it's a 'valid' substring according to the problem's rules. The problem defines what a valid substring is.
  3. Now, consider groups of substrings. Explore all possible combinations of these substrings to see if they overlap. If any substrings in a group overlap, discard that group.
  4. Count the number of non-overlapping substrings in each of the remaining groups.
  5. Finally, compare the counts from all the valid, non-overlapping groups and select the group with the largest number of substrings. That group is the solution.

Code Implementation

def max_non_overlapping_substrings_brute_force(input_string):
    all_substrings = []
    string_length = len(input_string)

    # Generate all possible substrings.
    for i in range(string_length):
        for j in range(i, string_length):
            substring = input_string[i:j+1]
            all_substrings.append(substring)

    max_substring_count = 0
    best_substring_combination = []

    # Iterate through all combinations of substrings.
    for i in range(1 << len(all_substrings)):
        current_substring_combination = []
        for j in range(len(all_substrings)):
            if (i >> j) & 1:
                current_substring_combination.append(all_substrings[j])

        is_valid = True
        for index_first in range(len(current_substring_combination)):
            for index_second in range(index_first + 1, len(current_substring_combination)):
                first_substring = current_substring_combination[index_first]
                second_substring = current_substring_combination[index_second]
                
                # Check for overlaps and invalidate combination if they exist.
                if not (first_substring[-1] < second_substring[0] or second_substring[-1] < first_substring[0]):
                    if (input_string.find(first_substring) < input_string.find(second_substring) + len(second_substring) and input_string.find(first_substring) + len(first_substring) > input_string.find(second_substring)) or (input_string.find(second_substring) < input_string.find(first_substring) + len(first_substring) and input_string.find(second_substring) + len(second_substring) > input_string.find(first_substring)):
                        is_valid = False
                        break

            if not is_valid:
                break

        # If the substring combination is valid (non-overlapping).
        if is_valid:

            #Update result if current combination has more substrings
            if len(current_substring_combination) > max_substring_count:
                max_substring_count = len(current_substring_combination)
                best_substring_combination = current_substring_combination

    return best_substring_combination

Big(O) Analysis

Time Complexity
O(3^n)The brute force approach considers all possible substrings. Generating all substrings takes O(n^2), where n is the length of the input string. Then, for each possible combination of these substrings, it checks for validity and overlap, and counts the maximum number of non-overlapping substrings. Generating all possible combinations of substrings involves exploring all possible subsets of substrings. The number of subsets of a set with size 'm' is 2^m. In the worst case, the number of possible substrings 'm' would still be related to 'n' creating an overall complexity of O(2^(n^2)). However, a tighter bound considers that for each character, we have 3 choices: the character could be at the beginning of a substring, in the middle of a substring, or not present in any substring. This leads to a complexity of O(3^n).
Space Complexity
O(N^2)The algorithm considers every possible substring, which requires generating and potentially storing them. In the worst case, the number of possible substrings of a string of length N is N(N+1)/2, which is proportional to N^2. Storing these substrings, or even the indices representing their start and end points, would require auxiliary space proportional to the number of substrings considered. Therefore, the space complexity is O(N^2). The algorithm also involves storing groups of substrings, further contributing to O(N^2) space in the worst-case scenario.

Optimal Solution

Approach

The goal is to find the largest number of non-overlapping groups of letters within a larger string. Instead of checking every possible group, we cleverly identify the smallest possible groups first, then combine them if possible, ensuring no groups overlap.

Here's how the algorithm would work step-by-step:

  1. First, figure out the earliest and latest position of each letter in the string.
  2. Then, go through the string from left to right. For each letter, find the group of letters that starts at that letter and extends as little as possible but still contains all positions of all letters within that initial group.
  3. If a smaller group is entirely contained within a larger group, then replace the larger group with the smaller group.
  4. Once we have the smallest possible groups, sort them based on their ending positions.
  5. Finally, select groups in order. If the current group does not overlap with the previously selected group, add it to the result. If they overlap, skip it.
  6. The selected groups will be the maximum number of non-overlapping substrings.

Code Implementation

def max_num_of_substrings(input_string):
    first_occurrence = {}
    last_occurrence = {}

    for index, char in enumerate(input_string):
        if char not in first_occurrence:
            first_occurrence[char] = index
        last_occurrence[char] = index

    substrings = []
    current_start = -1
    current_end = -1

    for index, char in enumerate(input_string):
        if index < current_start:
            continue

        substring_end = last_occurrence[char]

        # Expand substring to include all chars
        maximum_end = substring_end
        current_index = index

        while current_index <= maximum_end:
            char_at_index = input_string[current_index]
            maximum_end = max(maximum_end, last_occurrence[char_at_index])
            current_index += 1

        is_valid = True
        current_index = index
        while current_index <= maximum_end:
            char_at_index = input_string[current_index]
            if first_occurrence[char_at_index] < index:
                is_valid = False
                break
            current_index += 1

        if is_valid:
            substrings.append((index, maximum_end))
            current_start = index
            current_end = maximum_end

    substrings.sort(key=lambda x: x[1])
    result = []
    last_end = -1

    # Iterate to build result, non-overlapping substrings
    for start, end in substrings:
        if start > last_end:

            # Avoid overlapping, choose the one that ends earliest.
            result.append(input_string[start : end + 1])
            last_end = end

    return result

Big(O) Analysis

Time Complexity
O(n)The algorithm iterates through the string of length n multiple times, but each iteration performs a specific task with linear complexity. Finding the earliest and latest positions of each character takes O(n). Identifying and potentially shrinking the groups to the smallest possible size also involves iterating through the string at most a few times which is still O(n). Sorting the groups based on their ending positions is O(k log k) where k <= n, thus O(n log n). Finally, selecting non-overlapping groups involves another linear scan O(n). Therefore, the most significant term dominates, and the overall time complexity is effectively O(n log n) which is most commonly simplified to O(n) because the operations are very limited and would not drastically affect the runtime.
Space Complexity
O(1)The algorithm uses a fixed number of variables to store the earliest and latest positions of each letter (first_occurence and last_occurence, which can be considered constant since there are only 26 letters), and a list to store the final non-overlapping substrings, along with intermediate variables during processing. The number of possible substrings is limited, even if the input string's size (N) is very large. Therefore, the dominant space usage is the fixed-size data structures leading to constant space complexity, not dependent on N. Furthermore, sorting can be done in-place or with constant space depending on the implementation. Because the potential number of letters and substrings is independent of the input string's length, the auxiliary space remains constant. Thus, the space complexity is O(1).

Edge Cases

Empty string input
How to Handle:
Return an empty list as there are no substrings to extract.
String with only one character
How to Handle:
Return a list containing the string itself as a single non-overlapping substring.
String where no valid non-overlapping substrings can be formed (e.g., 'abab')
How to Handle:
Return an empty list when no valid substrings that meet the non-overlapping criteria are found.
Very long string with many characters to test scalability
How to Handle:
The solution should ideally use a linear-time or n log n algorithm to avoid timeouts with large input sizes.
String with all identical characters (e.g., 'aaaaaa')
How to Handle:
Return the entire string as a single substring since it's already non-overlapping.
Overlapping substring possibilities where greedy choice fails
How to Handle:
Ensure that the greedy choice made at each step leads to the globally optimal number of substrings by considering future characters before locking in a substring.
String containing repeating patterns that may or may not be non-overlapping.
How to Handle:
The algorithm should correctly identify and group characters of same type when forming substrings based on their first and last occurences.
String where the last occurrence of one character appears before the first occurrence of another, seemingly allowing for easy substrings.
How to Handle:
Algorithm needs to account for characters in between these extreme indices and extend the interval if necessary.