top of page
Search

Maximum Number of Non-Overlapping Substrings

Updated: Mar 25, 2021

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.

Solution:

     class Solution {
    public List<String> maxNumOfSubstrings(String s) {
        List<String> result = new ArrayList<>();
        Map<Character,int[]> map = new HashMap<>();
        for(int i=0;i<s.length();i++)
        {
            if(map.containsKey(s.charAt(i)))
            {
                map.get(s.charAt(i))[1]=i;
            }
            else
                map.put(s.charAt(i),new int[]{i,i});
        }
        int substringStart = -1;
        for(int i=0;i<s.length();i++)
        {
            int start = map.get(s.charAt(i))[0];
            if(start==i)
            {
                int substringEnd = getEnd(s,map,i);
                if(substringEnd!=-1)
                {
                    if(substringEnd>substringStart)
                    {
                        result.add("");
                    }
                    substringStart = substringEnd;
                    result.set(result.size()-1,s.substring(i,substringStart+1));
                }
            }
        }
            return result;
        }
        
    
    public int getEnd(String s, Map<Character,int[]> map, int start)
    {
        int end = map.get(s.charAt(start))[1];
        for(int i=start;i<end;i++)
        {
             if(map.get(s.charAt(i))[0]<start) return -1;
            end = Math.max(end,map.get(s.charAt(i))[1]);
        }
        return end;
    }
}


600 views0 comments

Recent Posts

See All

Minimum Deletions to Make Character Frequencies Unique

A string s is called good if there are no two different characters in s that have the same frequency. Given a string s, return the minimum number of characters you need to delete to make s good. The f

Smallest String With A Given Numeric Value

The numeric value of a lowercase character is defined as its position (1-indexed) in the alphabet, so the numeric value of a is 1, the numeric value of b is 2, the numeric value of c is 3, and so on.

bottom of page