Skip to content

2375. Construct Smallest Number From DI String #1330

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to construct the lexicographically smallest possible number string based on a given pattern of 'I' (increasing) and 'D' (decreasing) characters. The solution must ensure that each digit from 1 to 9 is used exactly once and that the sequence adheres to the given pattern.

Approach

The key insight is to recognize that consecutive 'D' characters in the pattern require a decreasing sequence of digits. By reversing segments of an initially increasing sequence of digits whenever a group of consecutive 'D's is encountered, we can efficiently generate the required sequence. This approach ensures that we produce the lexicographically smallest sequence by leveraging the properties of reversi…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Feb 18, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Feb 18, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants