<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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;aabb&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong>
We can obtain two palindromes from s, &quot;abba&quot; and &quot;baab&quot;. 
- We can obtain &quot;abba&quot; from s in 2 moves: &quot;a<u><strong>ab</strong></u>b&quot; -&gt; &quot;ab<u><strong>ab</strong></u>&quot; -&gt; &quot;abba&quot;.
- We can obtain &quot;baab&quot; from s in 2 moves: &quot;a<u><strong>ab</strong></u>b&quot; -&gt; &quot;<u><strong>ab</strong></u>ab&quot; -&gt; &quot;baab&quot;.
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 = &quot;letelt&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong>
One of the palindromes we can obtain from s in 2 moves is &quot;lettel&quot;.
One of the ways we can obtain it is &quot;lete<u><strong>lt</strong></u>&quot; -&gt; &quot;let<u><strong>et</strong></u>l&quot; -&gt; &quot;lettel&quot;.
Other palindromes such as &quot;tleelt&quot; 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>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 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>

class Solution:
    def minMovesToMakePalindrome(self, s: str) -> int:
        # https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/solutions/2604630/python3-easy-to-understand-2-pointer-solution-with-comments/
        # https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/solutions/2812340/python-2-pointers-explanation/
        # https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/solutions/3197767/python3-with-lots-of-comments/
        # Convert the string into a list for easy manipulation.
        char_list = list(s)
        # Initialize the number of moves and the indexes.
        moves = 0
        n = len(s)
        left = 0
        right = n - 1

        # Iterate until we have checked all characters.
        while left < right:
            # Flag to check if we found a matching character.
            found = False

            # Iterate from right to left starting from the current right index,
            # looking for a matching character.
            for k in range(right, left, -1):
                if char_list[left] == char_list[k]:
                    # When we find a match, we set found to True.
                    found = True
                    # We then move the matched character to its correct position
                    # by swapping adjacent characters.
                    while k < right:
                        char_list[k], char_list[k + 1] = char_list[k + 1], char_list[k]
                        k += 1
                        # Increment the moves count for each swap.
                        moves += 1
                    # Once we have moved the character to the correct position,
                    # we decrement the right index.
                    right -= 1
                    break

            # If we did not find a matching character, it means
            # the character needs to be moved to the center for odd length palindrome.
            if not found:
                # For this unmatched character, how many moves will it take to reach the center?
                # It will be half the length of the string minus the current index.
                moves += (n // 2) - left

            # Move to the next character from the left.
            left += 1

        # Return the total number of moves required to form a palindrome.
        return moves
16400