|
| 1 | +<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> |
| 2 | + |
| 3 | +<p>A <strong>0-indexed</strong> string <code>num</code> of length <code>n + 1</code> is created using the following conditions:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <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> |
| 7 | + <li>If <code>pattern[i] == 'I'</code>, then <code>num[i] < num[i + 1]</code>.</li> |
| 8 | + <li>If <code>pattern[i] == 'D'</code>, then <code>num[i] > num[i + 1]</code>.</li> |
| 9 | +</ul> |
| 10 | + |
| 11 | +<p>Return <em>the lexicographically <strong>smallest</strong> possible string </em><code>num</code><em> that meets the conditions.</em></p> |
| 12 | + |
| 13 | +<p> </p> |
| 14 | +<p><strong class="example">Example 1:</strong></p> |
| 15 | + |
| 16 | +<pre> |
| 17 | +<strong>Input:</strong> pattern = "IIIDIDDD" |
| 18 | +<strong>Output:</strong> "123549876" |
| 19 | +<strong>Explanation: |
| 20 | +</strong>At indices 0, 1, 2, and 4 we must have that num[i] < num[i+1]. |
| 21 | +At indices 3, 5, 6, and 7 we must have that num[i] > num[i+1]. |
| 22 | +Some possible values of num are "245639871", "135749862", and "123849765". |
| 23 | +It can be proven that "123549876" is the smallest possible num that meets the conditions. |
| 24 | +Note that "123414321" is not possible because the digit '1' is used more than once.</pre> |
| 25 | + |
| 26 | +<p><strong class="example">Example 2:</strong></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<strong>Input:</strong> pattern = "DDD" |
| 30 | +<strong>Output:</strong> "4321" |
| 31 | +<strong>Explanation:</strong> |
| 32 | +Some possible values of num are "9876", "7321", and "8742". |
| 33 | +It can be proven that "4321" is the smallest possible num that meets the conditions. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | +<p><strong>Constraints:</strong></p> |
| 38 | + |
| 39 | +<ul> |
| 40 | + <li><code>1 <= pattern.length <= 8</code></li> |
| 41 | + <li><code>pattern</code> consists of only the letters <code>'I'</code> and <code>'D'</code>.</li> |
| 42 | +</ul> |
0 commit comments