Advertisement

Construct Smallest Number From DI String - LeetCode 2375 Solution

Construct Smallest Number From DI String - Complete Solution Guide

Construct Smallest Number From DI String is LeetCode problem 2375, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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] . If pattern[i] == 'D' , then num[i] > num[i + 1] . Return the lexicographically smallest possible string num that meets the conditions. Example 1: Input: pa

Detailed Explanation

The problem asks us to construct the lexicographically smallest string of digits from 1 to n+1 (where n is the length of the input pattern string) such that the string satisfies the 'increasing' ('I') and 'decreasing' ('D') relationships defined by the input 'pattern' string. The resulting string must contain each digit from 1 to n+1 exactly once. If the pattern character at index `i` is 'I', then the digit at index `i` in the output string must be less than the digit at index `i+1`. If the pattern character is 'D', the digit at index `i` must be greater than the digit at index `i+1`.

Solution Approach

The provided code utilizes a stack-based approach. It iterates through the 'pattern' string and maintains a stack of numbers from 1 to n+1. Each number is pushed onto the stack. When an 'I' is encountered (meaning an increasing sequence is expected) or the end of the string is reached, the numbers from the stack are popped and appended to the result string. This reverses the order of numbers in the stack, effectively creating a decreasing sequence before the 'I'. This ensures that the smallest possible numbers are used in ascending order where needed and larger numbers are placed before smaller numbers where a decrease is needed.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty result string/list and an empty stack.
  2. Step 2: Iterate through the input 'pattern' string (including a virtual end-of-string position).
  3. Step 3: For each iteration, push the number (i+1) onto the stack (where 'i' is the current index).
  4. Step 4: Check if the current character in 'pattern' is 'I' or if the end of the pattern is reached. If either condition is true, proceed to Step 5.
  5. Step 5: While the stack is not empty, pop elements from the stack and append them to the result string.
  6. Step 6: Continue the loop until all elements in the 'pattern' have been processed.
  7. Step 7: Return the final result string.

Key Insights

  • Insight 1: The core idea is to use a stack to delay the assignment of numbers, strategically releasing them in the correct order to achieve the smallest possible number while adhering to the I/D pattern.
  • Insight 2: The stack helps to efficiently handle sequences of 'D' characters. When an 'I' is encountered or the end of the pattern is reached, the stack is popped, ensuring the 'decreasing' sequence is reversed and inserted into the result.
  • Insight 3: Building the string incrementally using a stack ensures that when we encounter an 'I', we can construct the smallest increasing sequence by popping elements from the stack that are greater than the next available digit.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: String, Backtracking, Stack, Greedy.

Companies

Asked at: Goldman Sachs, josh technology.