java.lang.Object
g2401_2500.s2430_maximum_deletions_on_a_string.Solution

public class Solution extends Object
2430 - Maximum Deletions on a String.<p>Hard</p> <p>You are given a string <code>s</code> consisting of only lowercase English letters. In one operation, you can:</p> <ul> <li>Delete <strong>the entire string</strong> <code>s</code>, or</li> <li>Delete the <strong>first</strong> <code>i</code> letters of <code>s</code> if the first <code>i</code> letters of <code>s</code> are <strong>equal</strong> to the following <code>i</code> letters in <code>s</code>, for any <code>i</code> in the range <code>1 <= i <= s.length / 2</code>.</li> </ul> <p>For example, if <code>s = &quot;ababc&quot;</code>, then in one operation, you could delete the first two letters of <code>s</code> to get <code>&quot;abc&quot;</code>, since the first two letters of <code>s</code> and the following two letters of <code>s</code> are both equal to <code>&quot;ab&quot;</code>.</p> <p>Return <em>the <strong>maximum</strong> number of operations needed to delete all of</em> <code>s</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;abcabcdabc&rdquo;</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>Delete the first 3 letters (&ldquo;abc&rdquo;) since the next 3 letters are equal. Now, s = &ldquo;abcdabc&rdquo;.</p> </li> <li> <p>Delete all the letters.</p> </li> </ul> <p>We used 2 operations so return 2. It can be proven that 2 is the maximum number of operations needed.</p> <p>Note that in the second operation we cannot delete &ldquo;abc&rdquo; again because the next occurrence of &ldquo;abc&rdquo; does not happen in the next 3 letters.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;aaabaab&rdquo;</p> <p><strong>Output:</strong> 4</p> <h2 id="explanation"><a href="#explanation" id="explanation"><strong>Explanation:</strong></a></h2> <ul> <li> <p>Delete the first letter (&ldquo;a&rdquo;) since the next letter is equal. Now, s = &ldquo;aabaab&rdquo;.</p> </li> <li> <p>Delete the first 3 letters (&ldquo;aab&rdquo;) since the next 3 letters are equal. Now, s = &ldquo;aab&rdquo;.</p> </li> <li> <p>Delete the first letter (&ldquo;a&rdquo;) since the next letter is equal. Now, s = &ldquo;ab&rdquo;.</p> </li> <li> <p>Delete all the letters.</p> </li> </ul> <p>We used 4 operations so return 4. It can be proven that 4 is the maximum number of operations needed.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;aaaaa&rdquo;</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> In each operation, we can delete the first letter of s.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 4000</code></li> <li><code>s</code> consists only of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • deleteString

      public int deleteString(String s)