The Wayback Machine - https://web.archive.org/web/20210131131219/https://www.geeksforgeeks.org/minimum-swaps-required-between-two-strings-to-make-one-string-strictly-greater-than-the-other/
Skip to content
Related Articles

Related Articles

Minimum swaps required between two strings to make one string strictly greater than the other
  • Last Updated : 25 Jan, 2021

Given two strings A and B of length M and N respectively, the task is to find the minimize swapping of two characters required to make the string A lexicographically greater than the string B.

Examples:

Input: A = “1432”, B = “789”, M = 4, N = 3
Output: 1
Explanation:
One possible way is to swap characters at index 0 of both the strings. Therefore, A modifies to “7432” and B modifies to “189”.

Input: A = “3733”, B = “3333”, M = 4, N = 4
Output: 2
Explanation:
Step 1:Swap character at index 1 of string A with character at index 0 of string B. The strings A and B modifies to “3333” and “7333”.
Step 2: Swap the character at index 0 of string A with character at index 0 of string B. The strings A and B modifies to “7333” and “3333”.

Approach: It can be observed that if M ≤ N and all the characters are the same including both strings, then it is not possible to make string A strictly greater than the string B. Otherwise, string A can be made strictly greater than string B by placing the two different characters at 0th index of both strings in a maximum of two moves.



Follow the steps below to solve the problem:

  1. First, check if the first character of string A is greater than the first character of string B then print 0.
  2. Otherwise, check if B[0] > A[0] then 1 swap is needed so swap A[0] with B[0] and print 1.
  3. Otherwise, check if all the characters are the same in both strings and M ≤ N then it is not possible so print -1.
  4. Otherwise, check if there lies any character in A which is smaller than A[0] or a character in B which is greater than B[0] then print 1.
  5. Otherwise, check if there exists any character in A which is less than A[0] or any character in B which is greater than B[0] then print 2.
  6. Otherwise, return 0 if none of the above condition satisfies.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum
// number of steps to make A > B
int minSteps(string A, string B, int M, int N)
{
 
    if (A[0] > B[0])
        return 0;
 
    if (B[0] > A[0]) {
        return 1;
    }
 
    // If all character are same and M <= N
    if (M <= N && A[0] == B[0]
        && count(A.begin(), A.end(), A[0]) == M
        && count(B.begin(), B.end(), B[0]) == N)
        return -1;
 
    // If there lies any character
    // in B which is greater than B[0]
    for (int i = 1; i < N; i++) {
 
        if (B[i] > B[0])
            return 1;
    }
 
    // If there lies any character
    // in A which is smaller than A[0]
    for (int i = 1; i < M; i++) {
 
        if (A[i] < A[0])
            return 1;
    }
 
    // If there lies a character which
    // is in A and greater than A[0]
    for (int i = 1; i < M; i++) {
 
        if (A[i] > A[0]) {
 
            swap(A[i], B[0]);
            swap(A[0], B[0]);
            return 2;
        }
    }
 
    // If there lies a character which
    // is in B and less than B[0]
    for (int i = 1; i < N; i++) {
 
        if (B[i] < B[0]) {
 
            swap(A[0], B[i]);
            swap(A[0], B[0]);
            return 2;
        }
    }
 
    // Otherwise
    return 0;
}
 
// Driver Code
int main()
{
    string A = "adsfd";
    string B = "dffff";
 
    int M = A.length();
    int N = B.length();
 
    cout << minSteps(A, B, M, N);
 
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function to find the minimum
# number of steps to make A > B
def minSteps(A, B, M, N):
     
    if (A[0] > B[0]):
        return 0
 
    if (B[0] > A[0]):
        return 1
 
    # If all character are same and M <= N
    if (M <= N and A[0] == B[0] and
           A.count(A[0]) == M and
           B.count(B[0]) == N):
        return -1
 
    # If there lies any character
    # in B which is greater than B[0]
    for i in range(1, N):
        if (B[i] > B[0]):
            return 1
 
    # If there lies any character
    # in A which is smaller than A[0]
    for i in range(1, M):
        if (A[i] < A[0]):
            return 1
 
    # If there lies a character which
    # is in A and greater than A[0]
    for i in range(1, M):
        if (A[i] > A[0]):
            A[0], B[i] = B[i], A[0]
            A[0], B[0] = B[0], A[0]
            return 2
 
    # If there lies a character which
    # is in B and less than B[0]
    for i in range(1, N):
        if (B[i] < B[0]):
            A[0], B[i] = B[i], A[0]
            A[0], B[0] = B[0], A[0]
            return 2
 
    # Otherwise
    return 0
 
# Driver Code
if __name__ == '__main__':
     
    A = "adsfd"
    B = "dffff"
 
    M = len(A)
    N = len(B)
 
    print(minSteps(A, B, M, N))
 
# This code is contributed by mohit kumar 29

chevron_right


Output: 

1

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :