Skip to content

Commit 60d6fdc

Browse files
authored
Create 2375. Construct Smallest Number From DI String (#719)
2 parents 85c784f + 0db4839 commit 60d6fdc

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
bool isMatched(string s, string pattern) {
4+
int n = pattern.size();
5+
for (int i = 0; i < n; i++) {
6+
if (pattern[i] == 'I' && s[i] > s[i + 1] ||
7+
pattern[i] == 'D' && s[i] < s[i + 1]) {
8+
return false;
9+
}
10+
}
11+
return true;
12+
}
13+
string smallestNumber(string pattern) {
14+
int n = pattern.size();
15+
string ans = "";
16+
for (int i = 1; i <= n + 1; i++) {
17+
ans.push_back(i + '0');
18+
}
19+
20+
while (!isMatched(ans, pattern)) {
21+
next_permutation(ans.begin(), ans.end());
22+
}
23+
return ans;
24+
}
25+
};

0 commit comments

Comments
 (0)