Class Solution

java.lang.Object
g2701_2800.s2716_minimize_string_length.Solution

public class Solution extends Object
2716 - Minimize String Length.<p>Easy</p> <p>Given a <strong>0-indexed</strong> string <code>s</code>, repeatedly perform the following operation <strong>any</strong> number of times:</p> <ul> <li>Choose an index <code>i</code> in the string, and let <code>c</code> be the character in position <code>i</code>. <strong>Delete</strong> the <strong>closest occurrence</strong> of <code>c</code> to the <strong>left</strong> of <code>i</code> (if any) and the <strong>closest occurrence</strong> of <code>c</code> to the <strong>right</strong> of <code>i</code> (if any).</li> </ul> <p>Your task is to <strong>minimize</strong> the length of <code>s</code> by performing the above operation any number of times.</p> <p>Return <em>an integer denoting the length of the <strong>minimized</strong> string.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;aaabc&rdquo;</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> In this example, s is &ldquo;aaabc&rdquo;. We can start by selecting the character &lsquo;a&rsquo; at index 1. We then remove the closest &lsquo;a&rsquo; to the left of index 1, which is at index 0, and the closest &lsquo;a&rsquo; to the right of index 1, which is at index 2. After this operation, the string becomes &ldquo;abc&rdquo;. Any further operation we perform on the string will leave it unchanged. Therefore, the length of the minimized string is 3.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;cbbd&rdquo;</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> For this we can start with character &lsquo;b&rsquo; at index 1. There is no occurrence of &lsquo;b&rsquo; to the left of index 1, but there is one to the right at index 2, so we delete the &lsquo;b&rsquo; at index 2. The string becomes &ldquo;cbd&rdquo; and further operations will leave it unchanged. Hence, the minimized length is 3.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;dddaaa&rdquo;</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> For this, we can start with the character &lsquo;d&rsquo; at index 1. The closest occurrence of a &lsquo;d&rsquo; to its left is at index 0, and the closest occurrence of a &lsquo;d&rsquo; to its right is at index 2. We delete both index 0 and 2, so the string becomes &ldquo;daaa&rdquo;. In the new string, we can select the character &lsquo;a&rsquo; at index 2. The closest occurrence of an &lsquo;a&rsquo; to its left is at index 1, and the closest occurrence of an &lsquo;a&rsquo; to its right is at index 3. We delete both of them, and the string becomes &ldquo;da&rdquo;. We cannot minimize this further, so the minimized length is 2.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 100</code></li> <li><code>s</code> contains only lowercase English letters</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minimizedStringLength

      public int minimizedStringLength(String s)