java.lang.Object
g2601_2700.s2645_minimum_additions_to_make_valid_string.Solution

public class Solution extends Object
2645 - Minimum Additions to Make Valid String.<p>Medium</p> <p>Given a string <code>word</code> to which you can insert letters &ldquo;a&rdquo;, &ldquo;b&rdquo; or &ldquo;c&rdquo; anywhere and any number of times, return <em>the minimum number of letters that must be inserted so that <code>word</code> becomes <strong>valid</strong>.</em></p> <p>A string is called <strong>valid</strong> if it can be formed by concatenating the string &ldquo;abc&rdquo; several times.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> word = &ldquo;b&rdquo;</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> Insert the letter &ldquo;a&rdquo; right before &ldquo;b&rdquo;, and the letter &ldquo;c&rdquo; right next to &ldquo;a&rdquo; to obtain the valid string &ldquo;<strong>a</strong>b<strong>c</strong>&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> word = &ldquo;aaa&rdquo;</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> Insert letters &ldquo;b&rdquo; and &ldquo;c&rdquo; next to each &ldquo;a&rdquo; to obtain the valid string &ldquo;a<strong>bc</strong>a<strong>bc</strong>a<strong>bc</strong>&rdquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> word = &ldquo;abc&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> word is already valid. No modifications are needed.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= word.length <= 50</code></li> <li><code>word</code> consists of letters &ldquo;a&rdquo;, &ldquo;b&rdquo; and &ldquo;c&rdquo; only.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • addMinimum

      public int addMinimum(String word)