Construct Smallest Number From DI String LeetCode Solution

Problem – Construct Smallest Number From DI String LeetCode Solution

You are given a 0-indexed string pattern of length n consisting of the characters 'I' meaning increasing and 'D' meaning decreasing.

0-indexed string num of length n + 1 is created using the following conditions:

  • num consists of the digits '1' to '9', where each digit is used at most once.
  • If pattern[i] == 'I', then num[i] < num[i + 1].
  • If pattern[i] == 'D', then num[i] > num[i + 1].

Return the lexicographically smallest possible string num that meets the conditions.

Example 1:

Input: pattern = "IIIDIDDD"
Output: "123549876"
Explanation:
At indices 0, 1, 2, and 4 we must have that num[i] < num[i+1].
At indices 3, 5, 6, and 7 we must have that num[i] > num[i+1].
Some possible values of num are "245639871", "135749862", and "123849765".
It can be proven that "123549876" is the smallest possible num that meets the conditions.
Note that "123414321" is not possible because the digit '1' is used more than once.

Example 2:

Input: pattern = "DDD"
Output: "4321"
Explanation:
Some possible values of num are "9876", "7321", and "8742".
It can be proven that "4321" is the smallest possible num that meets the conditions.

Constraints:

  • 1 <= pattern.length <= 8
  • pattern consists of only the letters 'I' and 'D'.

Construct Smallest Number From DI String LeetCode Solution in Java

    public String smallestNumber(String s) {
        StringBuilder res = new StringBuilder(), stack = new StringBuilder();
        for (int i = 0; i <= s.length(); i++) {
            stack.append((char)('1' + i));
            if (i == s.length() || s.charAt(i) == 'I') {
                res.append(stack.reverse());
                stack = new StringBuilder();
            }
        }
        return res.toString();
    }

Construct Smallest Number From DI String LeetCode Solution in C++

   string smallestNumber(string s) {
        string res, stack;
        for (int i = 0; i <= s.length(); i++) {
            stack.push_back('1' + i);
            if (i == s.length() || s[i] == 'I') {
                while (!stack.empty()) {
                    res.push_back(stack.back());
                    stack.pop_back();
                }
            }
        }
        return res;
    }

Construct Smallest Number From DI String LeetCode Solution in Python

    def smallestNumber(self, s):
        res, stack = [], []
        for i,c in enumerate(s + 'I', 1):
            stack.append(str(i))
            if c == 'I':
                res += stack[::-1]
                stack = []
        return ''.join(res)

    def smallestNumber(self, s):
        res, lasti = [], 0
        for i,c in enumerate(s + 'I', 1):
            if c == 'I':
                res += range(i, lasti, -1)
                lasti = i
        return ''.join(map(str,res))

    def smallestNumber(self, s):
        res, pool = [], list('987654321')
        for v in s.split('I'):
            res += [pool.pop() for _ in range(len(v) + 1)][::-1]
        return ''.join(res)
Construct Smallest Number From DI String LeetCode Solution Review:

In our experience, we suggest you solve this Construct Smallest Number From DI String LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Construct Smallest Number From DI String LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Construct Smallest Number From DI String LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.