Maximum Number of Non-Overlapping Substrings

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[k..l], either j < k or i > l 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 <= 10^5

  • s contains only lowercase English letters.

class Solution {
    // Map of character -> Span of that character
    // (OR combined span of all the characters in between)
    Map<Character, int[]> map;

    public List<String> maxNumOfSubstrings(String s) {
        map = new HashMap<>();
        List<String> ans = new ArrayList<>();
        // First and last occurrence of each character
        int[] last = new int[26];
        Arrays.fill(last, Integer.MIN_VALUE);
        int[] first = new int[26];
        Arrays.fill(first, Integer.MAX_VALUE);
        for (int i = 0; i < s.length(); i++) {
            first[s.charAt(i) - 'a'] = Math.min(first[s.charAt(i) - 'a'], i);
            last[s.charAt(i) - 'a'] = Math.max(last[s.charAt(i) - 'a'], i);
        }
        // O(26*log26) => O(1)
        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> (a[1] - a[0]) - (b[1] - b[0]));
        for (int i = 0; i < 26; i++) {
            if (last[i] != Integer.MIN_VALUE) {
                if (map.containsKey(i + 'a'))
                    pq.add(map.get(i + 'a'));
                else {
                    // Worst case O(26*N)
                    helper(s, first, last, (char) (i + 'a'));
                    pq.add(map.get((char) (i + 'a')));
                }
            }
        }
        // Store used intervals, at most 26
        List<int[]> used = new ArrayList<>();
        // Greedily choose the smallest spans, if we can
        while (pq.size() != 0) {
            int[] interval = pq.poll();
            boolean overlapping = false;
            // O(26) in worst case
            for (int[] usedInterval : used) {
                if ((interval[1] >= usedInterval[0] && interval[1] <= usedInterval[1])
                        || (interval[0] >= usedInterval[0] && interval[0] <= usedInterval[1])
                        || (usedInterval[0] >= interval[0] && usedInterval[1] <= interval[1])) {
                    overlapping = true;
                    break;
                }
            }
            if (!overlapping) {
                ans.add(s.substring(interval[0], interval[1] + 1));
                used.add(interval);
            }
        }
        return ans;
    }

    public void helper(String s, int[] first, int[] last, char c) {
        int L = first[c - 'a'], R = last[c - 'a'];
        while (true) {
            int newL = L, newR = R;
            for (int i = L; i <= R; i++) {
                newL = Math.min(newL, first[s.charAt(i) - 'a']);
                newR = Math.max(newR, last[s.charAt(i) - 'a']);
            }
            // If the span doesn't change, then we can break this loop
            if (newL == L && newR == R)
                break;
            L = newL;
            R = newR;
        }
        map.put(c, new int[] { L, R });
    }
}

Last updated