
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to count number of minimum swaps required to make it palindrome in Python
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 −
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
- Related Articles
- Program to find minimum swaps required to make given anagram in python
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Program to count number of swaps required to group all 1s together in Python
- Program to find minimum number of characters to be added to make it palindrome in Python
- Program to count number of swaps required to change one list to another in Python?
- Program to check minimum number of characters needed to make string palindrome in Python
- Minimum swaps required to make a binary string alternating in C++
- Program to find minimum number of operations required to make one number to another in Python
- Minimum number of swaps required to sort an array in C++
- Program to find number of swaps required to sort the sequence in python
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Program to count number of flipping required to make all x before y in Python
- Program to count minimum number of operations to flip columns to make target in Python
- Program to find minimum number of operations required to make one string substring of other in Python
- Program to find minimum number of bricks required to make k towers of same height in Python
