java.lang.Object
g2101_2200.s2193_minimum_number_of_moves_to_make_palindrome.Solution

public class Solution extends Object
2193 - Minimum Number of Moves to Make Palindrome.<p>Hard</p> <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><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;aabb&rdquo;</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <p>We can obtain two palindromes from s, &ldquo;abba&rdquo; and &ldquo;baab&rdquo;.</p> <ul> <li> <p>We can obtain &ldquo;abba&rdquo; from s in 2 moves: &ldquo;a<strong>ab</strong>b&rdquo; -> &ldquo;ab<strong>ab</strong>&rdquo; -> &ldquo;abba&rdquo;.</p> </li> <li> <p>We can obtain &ldquo;baab&rdquo; from s in 2 moves: &ldquo;a<strong>ab</strong>b&rdquo; -> &ldquo;<strong>ab</strong>ab&rdquo; -> &ldquo;baab&rdquo;.</p> </li> </ul> <p>Thus, the minimum number of moves needed to make s a palindrome is 2.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;letelt&rdquo;</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <p>One of the palindromes we can obtain from s in 2 moves is &ldquo;lettel&rdquo;.</p> <p>One of the ways we can obtain it is &ldquo;lete<strong>lt</strong>&rdquo; -> &ldquo;let<strong>et</strong>l&rdquo; -> &ldquo;lettel&rdquo;.</p> <p>Other palindromes such as &ldquo;tleelt&rdquo; can also be obtained in 2 moves.</p> <p>It can be shown that it is not possible to obtain a palindrome in less than 2 moves.</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>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minMovesToMakePalindrome

      public int minMovesToMakePalindrome(String s)