3579. Minimum Steps to Convert String with Operations Hard

@problem@discussion
#String#Dynamic Programming#Greedy



1/**
2 * [3579] Minimum Steps to Convert String with Operations
3 *
4 * You are given two strings, word1 and word2, of equal length. You need to transform word1 into word2.
5 * For this, divide word1 into one or more contiguous <span data-keyword="substring-nonempty">substrings</span>. For each substring substr you can perform the following operations:
6 * <ol>
7 * 	
8 * 	Replace: Replace the character at any one index of substr with another lowercase English letter.
9 * 	
10 * 	
11 * 	Swap: Swap any two characters in substr.
12 * 	
13 * 	
14 * 	Reverse Substring: Reverse substr.
15 * 	
16 * </ol>
17 * Each of these counts as one operation and each character of each substring can be used in each type of operation at most once (i.e. no single index may be involved in more than one replace, one swap, or one reverse).
18 * Return the minimum number of operations required to transform word1 into word2.
19 *  
20 * <strong class="example">Example 1:
21 * <div class="example-block">
22 * Input: <span class="example-io">word1 = "abcdf", word2 = "dacbe"</span>
23 * Output: <span class="example-io">4</span>
24 * Explanation:
25 * Divide word1 into "ab", "c", and "df". The operations are:
26 * 
27 * 	For the substring "ab",
28 * 	
29 * 		Perform operation of type 3 on "ab" -> "ba".
30 * 		Perform operation of type 1 on "ba" -> "da".
31 * 	
32 * 	
33 * 	For the substring "c" do no operations.
34 * 	For the substring "df",
35 * 	
36 * 		Perform operation of type 1 on "df" -> "bf".
37 * 		Perform operation of type 1 on "bf" -> "be".
38 * 	
39 * 	
40 * </div>
41 * <strong class="example">Example 2:
42 * <div class="example-block">
43 * Input: <span class="example-io">word1 = "abceded", word2 = "baecfef"</span>
44 * Output: <span class="example-io">4</span>
45 * Explanation:
46 * Divide word1 into "ab", "ce", and "ded". The operations are:
47 * 
48 * 	For the substring "ab",
49 * 	
50 * 		Perform operation of type 2 on "ab" -> "ba".
51 * 	
52 * 	
53 * 	For the substring "ce",
54 * 	
55 * 		Perform operation of type 2 on "ce" -> "ec".
56 * 	
57 * 	
58 * 	For the substring "ded",
59 * 	
60 * 		Perform operation of type 1 on "ded" -> "fed".
61 * 		Perform operation of type 1 on "fed" -> "fef".
62 * 	
63 * 	
64 * </div>
65 * <strong class="example">Example 3:
66 * <div class="example-block">
67 * Input: <span class="example-io">word1 = "abcdef", word2 = "fedabc"</span>
68 * Output: <span class="example-io">2</span>
69 * Explanation:
70 * Divide word1 into "abcdef". The operations are:
71 * 
72 * 	For the substring "abcdef",
73 * 	
74 * 		Perform operation of type 3 on "abcdef" -> "fedcba".
75 * 		Perform operation of type 2 on "fedcba" -> "fedabc".
76 * 	
77 * 	
78 * </div>
79 *  
80 * Constraints:
81 * 
82 * 	1 <= word1.length == word2.length <= 100
83 * 	word1 and word2 consist only of lowercase English letters.
84 * 
85 */
86pub struct Solution {}
87
88// problem: https://leetcode.com/problems/minimum-steps-to-convert-string-with-operations/
89// discuss: https://leetcode.com/problems/minimum-steps-to-convert-string-with-operations/discuss/?currentPage=1&orderBy=most_votes&query=
90
91// submission codes start here
92
93impl Solution {
94    pub fn min_operations(word1: String, word2: String) -> i32 {
95        0
96    }
97}
98
99// submission codes end
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn test_3579() {
107    }
108}
109

Back
© 2026 bowen.ge All Rights Reserved.