2375. Construct Smallest Number From DI String LeetCode Solution

In this guide, you will get 2375. Construct Smallest Number From DI String LeetCode Solution with the best time and space complexity. The solution to Construct Smallest Number From DI String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Construct Smallest Number From DI String solution in C++
  4. Construct Smallest Number From DI String solution in Java
  5. Construct Smallest Number From DI String solution in Python
  6. Additional Resources
2375. Construct Smallest Number From DI String LeetCode Solution image

Problem Statement of Construct Smallest Number From DI String

You are given a 0-indexed string pattern of length n consisting of the characters ‘I’ meaning increasing and ‘D’ meaning decreasing.
A 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].

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].
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.

See also  828. Count Unique Characters of All Substrings of a Given String LeetCode Solution

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

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

class Solution {
 public:
  string smallestNumber(string pattern) {
    string ans;
    stack<char> stack{{'1'}};

    for (const char c : pattern) {
      char maxSorFar = stack.top();
      if (c == 'I')
        while (!stack.empty()) {
          maxSorFar = max(maxSorFar, stack.top());
          ans += stack.top(), stack.pop();
        }
      stack.push(maxSorFar + 1);
    }

    while (!stack.empty())
      ans += stack.top(), stack.pop();

    return ans;
  }
};
/* code provided by PROGIEZ */

2375. Construct Smallest Number From DI String LeetCode Solution in Java

class Solution {
  public String smallestNumber(String pattern) {
    StringBuilder sb = new StringBuilder();
    Deque<Character> stack = new ArrayDeque<>(List.of('1'));

    for (final char c : pattern.toCharArray()) {
      char maxSorFar = stack.peek();
      if (c == 'I')
        while (!stack.isEmpty()) {
          maxSorFar = (char) Math.max(maxSorFar, stack.peek());
          sb.append(stack.pop());
        }
      stack.push((char) (maxSorFar + 1));
    }

    while (!stack.isEmpty())
      sb.append(stack.pop());

    return sb.toString();
  }
}
// code provided by PROGIEZ

2375. Construct Smallest Number From DI String LeetCode Solution in Python

class Solution:
  def smallestNumber(self, pattern: str) -> str:
    ans = []
    stack = ['1']

    for c in pattern:
      maxSorFar = stack[-1]
      if c == 'I':
        while stack:
          maxSorFar = max(maxSorFar, stack[-1])
          ans.append(stack.pop())
      stack.append(chr(ord(maxSorFar) + 1))

    while stack:
      ans.append(stack.pop())

    return ''.join(ans)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.