Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a17c5e6

Browse files
committed
2375
1 parent e38db0d commit a17c5e6

File tree

1 file changed

+32
-0
lines changed
  • Medium/2375. Construct Smallest Number From DI String

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
class Solution
3+
{
4+
/**
5+
* @param String $pattern
6+
* @return String
7+
*/
8+
public function smallestNumber($pattern)
9+
{
10+
$stack = [];
11+
12+
$result = "";
13+
14+
$num = 1;
15+
16+
for ($i = 0; $i <= strlen($pattern); $i++) {
17+
18+
$stack[] = $num++;
19+
20+
if ($i == strlen($pattern) || $pattern[$i] == 'I') {
21+
while (! empty($stack)) {
22+
$result .= array_pop($stack);
23+
}
24+
}
25+
}
26+
return $result;
27+
}
28+
}
29+
30+
$solution = new Solution();
31+
32+
echo $solution->smallestNumber('IIIDIDDD'); // Output: "123"

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /