Introduction
Either it is an Online Assessment or a face-to-face interview. Both are incomplete without Backtracking. The basic concept of Backtracking remains the same, yet many questions are framed on this topic. Today we will see one such question to enrich your concepts about Backtracking, i.e., Minimize swaps of pairs of characters required required required such that no two adjacent characters in the string are the same.
But before preceding this, let’s see what Backtracking is.
Backtracking is a class of algorithms used for finding solutions to some computational problems or notably constraint satisfaction problems. We can also say that it is a technique for finding all the solutions to a given problem by rejecting the solutions that do not satisfy the given constraint.
You can also refer to the article, Introduction to Backtracking to know more about Backtracking.
Now let's discuss the problem statement in detail.
Problem Statement
You have been given a string. You have to find the minimum number of pairs of characters that need to be swapped such that after swapping, no two adjacent characters are the same. If it's not possible, then return '-1'.
Let's understand the problem. Minimize swaps of pairs of characters required by the following example.
Input
'STR’= EWEEHF
Output:
1
Explanation
If we swap the 3rd and 6th characters in the string, we will have "EWFEHE." There are no two adjacent characters in this string that are the same. It took us one swap. Thus, this is our answer.
Intuition
As we have to find the minimum number of operations required, we can generate all possible combinations and check which of them takes the minimum functions.
But how are we going to create that?
Backtracking? Yes.
Refer to this library, Backtracking for more information.
Let's look at the algorithm we used to solve the problem of Minimize swaps of pairs of characters required
Algorithm
- We will create a function, let's say minSwaps() which will take four arguments, namely the 'STR' (given string), 'ANS' (Final answer, initialized to INT_MAX), 'TEMP' (Swaps required in the particular recursive call), and 'RES' which will keep track of the index.
- In the function, We will first check if the string contains any adjacent characters which are the same. If they are not the same, then we will update the value of ANS as min(ANS, TEMP). We will iterate over the string using the variable 'i' from 'RES' to 'LEN' (Length of the string). Now we will perform the following operations.
- We will use a variable 'j' to iterate from i+1 to 'LEN,' then we will swap the characters at position' i' and 'j' and then call the function minSwaps(). But this time, we will increment the value of 'RES' to 'i’+1 and 'TEMP' to 'TEMP’+1 as we have made one swap.
- Now we will backtrack, i.e., swap the 'i's and 'j' characters again.
After the complete string is traversed, our final answer will be stored in 'ANS.' If the value of 'ANS' after the process is 'INT_MAX,' then we will print '-1'; otherwise, we will print the value of 'ANS.'
Let's see the implementation of the problem. Minimize swaps of pairs of characters required.