Program to count number of minimum swaps required to make it palindrome in Python

PythonServer Side ProgrammingProgramming

Suppose we have a string s, we have to find the minimum number of adjacent swaps needed to make it into a palindrome. If there is no such way to solve, then return -1.

So, if the input is like s = "xxyy", then the output will be 2, as we can swap the middle "x" and "y" so string is "xyxy" and then swap the first two "x" and "y" to get "yxxy", and this is palindrome.

To solve this, we will follow these steps −

  • Define a function util() . This will take s

  • seen := a new map

  • for each i in s, do

    • seen[i] := 1 + (seen[i] if exists otherwise 0)

  • odd_count := 0

  • for each key value pair of seen, do

    • if value is odd, then

      • odd_count := odd_count + 1

    • if odd_count is same as 2, then

      • return False

  • return True

  • From the main method do the following −

  • swaps := 0

  • if util(s) is true, then

    • left := 0

    • right := size of s - 1

    • s := a new list of characters by taking from s

    • while left < right, do

      • if s[left] is not same as s[right], then

        • k := right

        • while k > left and s[k] is not same as s[left], do

          • k := k - 1

        • if k is same as left, then

          • swaps := swaps + 1

          • s[left], s[left + 1] := s[left + 1], s[left]

        • otherwise,

          • while k < right, do

            • swap s[k], s[k + 1]

            • k := k + 1

            • swaps := swaps + 1

          • left := left + 1

          • right := right - 1

      • otherwise,

        • left := left + 1

        • right := right - 1

    • return swaps

  • return -1

Example(Python)

Let us see the following implementation to get better understanding −

 Live Demo

class Solution:
   def solve(self, s):
      def util(s):
         seen = {}
         for i in s:
            seen[i] = seen.get(i, 0) + 1
         odd_count = 0
         for k, val in seen.items():
            if val & 1 == 1:
               odd_count += 1
            if odd_count == 2:
               return False
         return True
      swaps = 0
      if util(s):
         left = 0
         right = len(s) - 1
         s = list(s)
         while left < right:
            if s[left] != s[right]:
               k = right
               while k > left and s[k] != s[left]:
                  k -= 1
               if k == left:
                  swaps += 1
                  s[left], s[left + 1] = s[left + 1], s[left]
               else:
                  while k < right:
                     s[k], s[k + 1] = s[k + 1], s[k]
                     k += 1
                     swaps += 1
                  left += 1
                  right -= 1
            else:
               left += 1
               right -= 1
         return swaps
      return -1
ob = Solution()
s = "xxyy"
print(ob.solve(s))

Input

"xxyy"

Output

2
raja
Published on 21-Dec-2020 14:06:15
Advertisements