2193. Minimum Number of Moves to Make Palindrome Hard

@problem@discussion
#Two Pointers#String#Greedy#Binary Indexed Tree



1/**
2 * [2193] Minimum Number of Moves to Make Palindrome
3 *
4 * You are given a string s consisting only of lowercase English letters.
5 * In one move, you can select any two adjacent characters of s and swap them.
6 * Return the minimum number of moves needed to make s a palindrome.
7 * Note that the input will be generated such that s can always be converted to a palindrome.
8 *  
9 * Example 1:
10 * 
11 * Input: s = "aabb"
12 * Output: 2
13 * Explanation:
14 * We can obtain two palindromes from s, "abba" and "baab". 
15 * - We can obtain "abba" from s in 2 moves: "a<u>ab</u>b" -> "ab<u>ab</u>" -> "abba".
16 * - We can obtain "baab" from s in 2 moves: "a<u>ab</u>b" -> "<u>ab</u>ab" -> "baab".
17 * Thus, the minimum number of moves needed to make s a palindrome is 2.
18 * 
19 * Example 2:
20 * 
21 * Input: s = "letelt"
22 * Output: 2
23 * Explanation:
24 * One of the palindromes we can obtain from s in 2 moves is "lettel".
25 * One of the ways we can obtain it is "lete<u>lt</u>" -> "let<u>et</u>l" -> "lettel".
26 * Other palindromes such as "tleelt" can also be obtained in 2 moves.
27 * It can be shown that it is not possible to obtain a palindrome in less than 2 moves.
28 * 
29 *  
30 * Constraints:
31 * 
32 * 	1 <= s.length <= 2000
33 * 	s consists only of lowercase English letters.
34 * 	s can be converted to a palindrome using a finite number of moves.
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/
40// discuss: https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn min_moves_to_make_palindrome(s: String) -> i32 {
46        0
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_2193() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.