2232. Minimize Result by Adding Parentheses to Expression Medium

@problem@discussion
#String#Enumeration



1/**
2 * [2232] Minimize Result by Adding Parentheses to Expression
3 *
4 * You are given a 0-indexed string expression of the form "<num1>+<num2>" where <num1> and <num2> represent positive integers.
5 * Add a pair of parentheses to expression such that after the addition of parentheses, expression is a valid mathematical expression and evaluates to the smallest possible value. The left parenthesis must be added to the left of '+' and the right parenthesis must be added to the right of '+'.
6 * Return expression after adding a pair of parentheses such that expression evaluates to the smallest possible value. If there are multiple answers that yield the same result, return any of them.
7 * The input has been generated such that the original value of expression, and the value of expression after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.
8 *  
9 * Example 1:
10 * 
11 * Input: expression = "247+38"
12 * Output: "2(47+38)"
13 * Explanation: The expression evaluates to 2 * (47 + 38) = 2 * 85 = 170.
14 * Note that "2(4)7+38" is invalid because the right parenthesis must be to the right of the '+'.
15 * It can be shown that 170 is the smallest possible value.
16 * 
17 * Example 2:
18 * 
19 * Input: expression = "12+34"
20 * Output: "1(2+3)4"
21 * Explanation: The expression evaluates to 1 * (2 + 3) * 4 = 1 * 5 * 4 = 20.
22 * 
23 * Example 3:
24 * 
25 * Input: expression = "999+999"
26 * Output: "(999+999)"
27 * Explanation: The expression evaluates to 999 + 999 = 1998.
28 * 
29 *  
30 * Constraints:
31 * 
32 * 	3 <= expression.length <= 10
33 * 	expression consists of digits from '1' to '9' and '+'.
34 * 	expression starts and ends with digits.
35 * 	expression contains exactly one '+'.
36 * 	The original value of expression, and the value of expression after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/minimize-result-by-adding-parentheses-to-expression/
42// discuss: https://leetcode.com/problems/minimize-result-by-adding-parentheses-to-expression/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn minimize_result(expression: String) -> String {
48        String::new()
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_2232() {
60    }
61}
62


Back
© 2025 bowen.ge All Rights Reserved.