Skip to content

Commit 9691d25

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 17.8 MB (38.19%)
1 parent 0211c86 commit 9691d25

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>You are given a <strong>0-indexed</strong> string <code>pattern</code> of length <code>n</code> consisting of the characters <code>&#39;I&#39;</code> meaning <strong>increasing</strong> and <code>&#39;D&#39;</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>&#39;1&#39;</code> to <code>&#39;9&#39;</code>, where each digit is used <strong>at most</strong> once.</li>
7+
<li>If <code>pattern[i] == &#39;I&#39;</code>, then <code>num[i] &lt; num[i + 1]</code>.</li>
8+
<li>If <code>pattern[i] == &#39;D&#39;</code>, then <code>num[i] &gt; 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>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> pattern = &quot;IIIDIDDD&quot;
18+
<strong>Output:</strong> &quot;123549876&quot;
19+
<strong>Explanation:
20+
</strong>At indices 0, 1, 2, and 4 we must have that num[i] &lt; num[i+1].
21+
At indices 3, 5, 6, and 7 we must have that num[i] &gt; num[i+1].
22+
Some possible values of num are &quot;245639871&quot;, &quot;135749862&quot;, and &quot;123849765&quot;.
23+
It can be proven that &quot;123549876&quot; is the smallest possible num that meets the conditions.
24+
Note that &quot;123414321&quot; is not possible because the digit &#39;1&#39; is used more than once.</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> pattern = &quot;DDD&quot;
30+
<strong>Output:</strong> &quot;4321&quot;
31+
<strong>Explanation:</strong>
32+
Some possible values of num are &quot;9876&quot;, &quot;7321&quot;, and &quot;8742&quot;.
33+
It can be proven that &quot;4321&quot; is the smallest possible num that meets the conditions.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= pattern.length &lt;= 8</code></li>
41+
<li><code>pattern</code> consists of only the letters <code>&#39;I&#39;</code> and <code>&#39;D&#39;</code>.</li>
42+
</ul>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def smallestNumber(self, pattern: str) -> str:
3+
result = []
4+
num_stack = []
5+
6+
# Iterate through the pattern
7+
for index in range(len(pattern) + 1):
8+
# Push the next number onto the stack
9+
num_stack.append(index + 1)
10+
11+
# If 'I' is encountered or we reach the end, pop all stack elements
12+
if index == len(pattern) or pattern[index] == "I":
13+
while num_stack:
14+
result.append(str(num_stack.pop()))
15+
16+
return "".join(result)

0 commit comments

Comments
 (0)