2375. Construct Smallest Number From DI String Medium

@problem@discussion
#String#Backtracking#Stack#Greedy



1/**
2 * [2375] Construct Smallest Number From DI String
3 *
4 * You are given a 0-indexed string pattern of length n consisting of the characters 'I' meaning increasing and 'D' meaning decreasing.
5 * A 0-indexed string num of length n + 1 is created using the following conditions:
6 * 
7 * 	num consists of the digits '1' to '9', where each digit is used at most once.
8 * 	If pattern[i] == 'I', then num[i] < num[i + 1].
9 * 	If pattern[i] == 'D', then num[i] > num[i + 1].
10 * 
11 * Return the lexicographically smallest possible string num that meets the conditions.
12 *  
13 * Example 1:
14 * 
15 * Input: pattern = "IIIDIDDD"
16 * Output: "123549876"
17 * Explanation:
18 * At indices 0, 1, 2, and 4 we must have that num[i] < num[i+1].
19 * At indices 3, 5, 6, and 7 we must have that num[i] > num[i+1].
20 * Some possible values of num are "245639871", "135749862", and "123849765".
21 * It can be proven that "123549876" is the smallest possible num that meets the conditions.
22 * Note that "123414321" is not possible because the digit '1' is used more than once.
23 * Example 2:
24 * 
25 * Input: pattern = "DDD"
26 * Output: "4321"
27 * Explanation:
28 * Some possible values of num are "9876", "7321", and "8742".
29 * It can be proven that "4321" is the smallest possible num that meets the conditions.
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	1 <= pattern.length <= 8
35 * 	pattern consists of only the letters 'I' and 'D'.
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/construct-smallest-number-from-di-string/
41// discuss: https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn smallest_number(pattern: String) -> String {
47        String::new()
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_2375() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.