mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			42 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			42 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>You are given a string <code>s</code> consisting only of lowercase English letters.</p>
 | 
						|
 | 
						|
<p>In one <strong>move</strong>, you can select any two <strong>adjacent</strong> characters of <code>s</code> and swap them.</p>
 | 
						|
 | 
						|
<p>Return <em>the <strong>minimum number of moves</strong> needed to make</em> <code>s</code> <em>a palindrome</em>.</p>
 | 
						|
 | 
						|
<p><strong>Note</strong> that the input will be generated such that <code>s</code> can always be converted to a palindrome.</p>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong class="example">Example 1:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> s = "aabb"
 | 
						|
<strong>Output:</strong> 2
 | 
						|
<strong>Explanation:</strong>
 | 
						|
We can obtain two palindromes from s, "abba" and "baab". 
 | 
						|
- We can obtain "abba" from s in 2 moves: "a<u><strong>ab</strong></u>b" -> "ab<u><strong>ab</strong></u>" -> "abba".
 | 
						|
- We can obtain "baab" from s in 2 moves: "a<u><strong>ab</strong></u>b" -> "<u><strong>ab</strong></u>ab" -> "baab".
 | 
						|
Thus, the minimum number of moves needed to make s a palindrome is 2.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong class="example">Example 2:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> s = "letelt"
 | 
						|
<strong>Output:</strong> 2
 | 
						|
<strong>Explanation:</strong>
 | 
						|
One of the palindromes we can obtain from s in 2 moves is "lettel".
 | 
						|
One of the ways we can obtain it is "lete<u><strong>lt</strong></u>" -> "let<u><strong>et</strong></u>l" -> "lettel".
 | 
						|
Other palindromes such as "tleelt" can also be obtained in 2 moves.
 | 
						|
It can be shown that it is not possible to obtain a palindrome in less than 2 moves.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>1 <= s.length <= 2000</code></li>
 | 
						|
	<li><code>s</code> consists only of lowercase English letters.</li>
 | 
						|
	<li><code>s</code> can be converted to a palindrome using a finite number of moves.</li>
 | 
						|
</ul>
 |