|
| 1 | +<p>Given a string <code>s</code> consisting only of characters <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>. You are asked to apply the following algorithm on the string any number of times:</p> |
| 2 | + |
| 3 | +<ol> |
| 4 | + <li>Pick a <strong>non-empty</strong> prefix from the string <code>s</code> where all the characters in the prefix are equal.</li> |
| 5 | + <li>Pick a <strong>non-empty</strong> suffix from the string <code>s</code> where all the characters in this suffix are equal.</li> |
| 6 | + <li>The prefix and the suffix should not intersect at any index.</li> |
| 7 | + <li>The characters from the prefix and suffix must be the same.</li> |
| 8 | + <li>Delete both the prefix and the suffix.</li> |
| 9 | +</ol> |
| 10 | + |
| 11 | +<p>Return <em>the <strong>minimum length</strong> of </em><code>s</code> <em>after performing the above operation any number of times (possibly zero times)</em>.</p> |
| 12 | + |
| 13 | +<p> </p> |
| 14 | +<p><strong class="example">Example 1:</strong></p> |
| 15 | + |
| 16 | +<pre> |
| 17 | +<strong>Input:</strong> s = "ca" |
| 18 | +<strong>Output:</strong> 2 |
| 19 | +<strong>Explanation: </strong>You can't remove any characters, so the string stays as is. |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p><strong class="example">Example 2:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +<strong>Input:</strong> s = "cabaabac" |
| 26 | +<strong>Output:</strong> 0 |
| 27 | +<strong>Explanation:</strong> An optimal sequence of operations is: |
| 28 | +- Take prefix = "c" and suffix = "c" and remove them, s = "abaaba". |
| 29 | +- Take prefix = "a" and suffix = "a" and remove them, s = "baab". |
| 30 | +- Take prefix = "b" and suffix = "b" and remove them, s = "aa". |
| 31 | +- Take prefix = "a" and suffix = "a" and remove them, s = "".</pre> |
| 32 | + |
| 33 | +<p><strong class="example">Example 3:</strong></p> |
| 34 | + |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> s = "aabccabba" |
| 37 | +<strong>Output:</strong> 3 |
| 38 | +<strong>Explanation:</strong> An optimal sequence of operations is: |
| 39 | +- Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb". |
| 40 | +- Take prefix = "b" and suffix = "bb" and remove them, s = "cca". |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | +<p><strong>Constraints:</strong></p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li><code>1 <= s.length <= 10<sup>5</sup></code></li> |
| 48 | + <li><code>s</code> only consists of characters <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</li> |
| 49 | +</ul> |
0 commit comments