Minimum swaps required between two strings to make one string strictly greater than the other


In this article, we'll discuss an intriguing problem in string manipulation - "Minimum swaps required between two strings to make one string strictly greater than the other". We'll understand the problem, detail a strategy to solve it, implement it in C++, and clarify the concept with a relevant example.

Understanding the Problem Statement

Given two strings of equal length, our goal is to determine the minimum number of character swaps required to make one string strictly greater than the other. The characters are swapped between the two strings, and each swap operation involves exactly one character from each string. The strings are compared lexicographically, where 'a' < 'b' < 'c', and so forth.

Approach

The idea is to use a greedy algorithm. We start from the beginning of the strings and, for each position, if the character in the first string is smaller than the corresponding character in the second string, we swap them. If they're equal, we look for a larger character in the second string to swap with. If no such character is found, we continue to the next position. We repeat this until we've processed all characters in the strings.

Example

Let's implement this approach in C++ −

#include<bits/stdc++.h>
using namespace std;

int minSwaps(string &s1, string &s2) {
   int swaps = 0;
   int n = s1.size();
   for(int i=0; i<n; i++) {
      if(s1[i] < s2[i]) {
         swap(s1[i], s2[i]);
         swaps++;
      }
      else if(s1[i] == s2[i]) {
         for(int j=i+1; j<n; j++) {
               if(s2[j] > s1[i]) {
                  swap(s1[i], s2[j]);
                  swaps++;
                  break;
               }
         }
      }
   }
   return (s1 > s2) ? swaps : -1;
}

int main() {
   string s1 = "bbca";
   string s2 = "abbc";
   int swaps = minSwaps(s1, s2);
   if(swaps != -1)
      cout << "Minimum swaps: " << swaps << "\n";
   else
      cout << "Cannot make string 1 greater\n";
   return 0;
}

Output

Minimum swaps: 2

Test Case

Let's consider the strings "bbca" and "abbc". The following swaps would occur −

  • Swap 'b' in the first string with 'a' in the second string. The strings now are "bbac" and "abbc".

  • Swap 'c' in the first string with 'b' in the second string. The strings now are "bbcb" and "abac".

"bbcb" is lexicographically greater than "abac". Therefore, the minimum number of swaps required is 2, and the output of the program will be "Minimum swaps: 2".

Conclusion

In this article, we explored the problem of determining the minimum number of swaps required between two strings to make one string lexicographically greater than the other. We discussed a strategy to solve the problem, implemented it in C++, and explained the concept with an example. String manipulation problems like these are common in interviews and competitive programming, and understanding these concepts can be quite beneficial.

Updated on: 18-May-2023

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements