Class Solution
java.lang.Object
g2301_2400.s2375_construct_smallest_number_from_di_string.Solution
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 = “IIIDIDDD”</p>
<p><strong>Output:</strong> “123549876”</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 “245639871”, “135749862”, and “123849765”.</p>
<p>It can be proven that “123549876” is the smallest possible num that meets the conditions.</p>
<p>Note that “123414321” is not possible because the digit ‘1’ is used more than once.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> pattern = “DDD”</p>
<p><strong>Output:</strong> “4321”</p>
<p><strong>Explanation:</strong></p>
<p>Some possible values of num are “9876”, “7321”, and “8742”.</p>
<p>It can be proven that “4321” 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 Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
smallestNumber
-