java.lang.Object
g1501_1600.s1520_maximum_number_of_non_overlapping_substrings.Solution

public class Solution extends Object
1520 - Maximum Number of Non-Overlapping Substrings.<p>Hard</p> <p>Given a string <code>s</code> of lowercase letters, you need to find the maximum number of <strong>non-empty</strong> substrings of <code>s</code> that meet the following conditions:</p> <ol> <li>The substrings do not overlap, that is for any two substrings <code>s[i..j]</code> and <code>s[k..l]</code>, either <code>j < k</code> or <code>i > l</code> is true.</li> <li>A substring that contains a certain character <code>c</code> must also contain all occurrences of <code>c</code>.</li> </ol> <p>Find <em>the maximum number of substrings that meet the above conditions</em>. 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.</p> <p>Notice that you can return the substrings in <strong>any</strong> order.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;adefaddaccc&rdquo;</p> <p><strong>Output:</strong> [&ldquo;e&rdquo;,&ldquo;f&rdquo;,&ldquo;ccc&rdquo;]</p> <p><strong>Explanation:</strong> The following are all the possible substrings that meet the conditions: [</p> <p>&ldquo;adefaddaccc&rdquo;</p> <p>&ldquo;adefadda&rdquo;,</p> <p>&ldquo;ef&rdquo;,</p> <p>&ldquo;e&rdquo;,</p> <p>&ldquo;f&rdquo;,</p> <p>&ldquo;ccc&rdquo;,</p> <p>]</p> <p>If we choose the first string, we cannot choose anything else and we&rsquo;d get only 1. If we choose &ldquo;adefadda&rdquo;, we are left with &ldquo;ccc&rdquo; which is the only one that doesn&rsquo;t overlap, thus obtaining 2 substrings. Notice also, that it&rsquo;s not optimal to choose &ldquo;ef&rdquo; since it can be split into two. Therefore, the optimal way is to choose [&ldquo;e&rdquo;,&ldquo;f&rdquo;,&ldquo;ccc&rdquo;] which gives us 3 substrings. No other solution of the same number of substrings exist.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;abbaccd&rdquo;</p> <p><strong>Output:</strong> [&ldquo;d&rdquo;,&ldquo;bb&rdquo;,&ldquo;cc&rdquo;]</p> <p><strong>Explanation:</strong> Notice that while the set of substrings [&ldquo;d&rdquo;,&ldquo;abba&rdquo;,&ldquo;cc&rdquo;] also has length 3, it&rsquo;s considered incorrect since it has larger total length.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 10^5</code></li> <li><code>s</code> contains only lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details