java.lang.Object
g2301_2400.s2375_construct_smallest_number_from_di_string.Solution

public class Solution extends Object
2375 - Construct Smallest Number From DI String.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> string <code>pattern</code> of length <code>n</code> consisting of the characters <code>'I'</code> meaning <strong>increasing</strong> and <code>'D'</code> meaning <strong>decreasing</strong>.</p> <p>A <strong>0-indexed</strong> string <code>num</code> of length <code>n + 1</code> is created using the following conditions:</p> <ul> <li><code>num</code> consists of the digits <code>'1'</code> to <code>'9'</code>, where each digit is used <strong>at most</strong> once.</li> <li>If <code>pattern[i] == 'I'</code>, then <code>num[i] < num[i + 1]</code>.</li> <li>If <code>pattern[i] == 'D'</code>, then <code>num[i] > num[i + 1]</code>.</li> </ul> <p>Return <em>the lexicographically <strong>smallest</strong> possible string</em> <code>num</code> <em>that meets the conditions.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> pattern = &ldquo;IIIDIDDD&rdquo;</p> <p><strong>Output:</strong> &ldquo;123549876&rdquo;</p> <p><strong>Explanation:</strong></p> <p>At indices 0, 1, 2, and 4 we must have that num[i] < num[i+1].</p> <p>At indices 3, 5, 6, and 7 we must have that num[i] > num[i+1].</p> <p>Some possible values of num are &ldquo;245639871&rdquo;, &ldquo;135749862&rdquo;, and &ldquo;123849765&rdquo;.</p> <p>It can be proven that &ldquo;123549876&rdquo; is the smallest possible num that meets the conditions.</p> <p>Note that &ldquo;123414321&rdquo; is not possible because the digit &lsquo;1&rsquo; is used more than once.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> pattern = &ldquo;DDD&rdquo;</p> <p><strong>Output:</strong> &ldquo;4321&rdquo;</p> <p><strong>Explanation:</strong></p> <p>Some possible values of num are &ldquo;9876&rdquo;, &ldquo;7321&rdquo;, and &ldquo;8742&rdquo;.</p> <p>It can be proven that &ldquo;4321&rdquo; is the smallest possible num that meets the conditions.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= pattern.length <= 8</code></li> <li><code>pattern</code> consists of only the letters <code>'I'</code> and <code>'D'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • smallestNumber

      public String smallestNumber(String pattern)