Lexicographically smaller string by swapping at most one character pair

Given two strings A and B of all uppercase letter, the task is to find whether is it possible to make string A strictly lexicographically smaller than string B by swapping at most one pair of characters in A.

Examples:

Input: A = “AGAIN”, B = “ACTION”
Output: Yes
Explanation:
We can make string A strictly lexicographically smaller than string B by swapping G and A (AAGIN)
AAGIN is lexicographically smaller than ACTION



Input: A = “APPLE” B = “AAAAAPPPLLE”
Output: No

Approach:

  1. Sort string A.
  2. We can find the first position where A and sorted(A) doesn’t match.
  3. We then find the letter that should be in that position and swap it with the letter in the sorted(A).
  4. If there are multiple choices, it is better to take the one that occurs last, since it makes the resulting string smallest.
  5. Now, compare string A and string B.

Below is the implementation of the above approach

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program check whether is
// it possible to make string A
// lexicographically smaller than string B
  
#include <bits/stdc++.h>
using namespace std;
  
// Swap function
void swap(char& x, char& y)
{
    char temp = x;
    x = y;
    y = temp;
}
  
// Function that finds whether is
// it possible to make string A
// lexicographically smaller than string B
bool IsLexicographicallySmaller(
    string A, string B)
{
    // Condition if string A
    // is already smaller than B
    if (A < B) {
        return true;
    }
    string temp = A;
  
    // Sorting temp string
    sort(temp.begin(), temp.end());
  
    int index = -1;
  
    for (int i = 0; i < A.length(); i++) {
        // Condition for first changed
        // character of string A and temp
        if (A[i] != temp[i]) {
            index = i;
            break;
        }
    }
  
    // Condition if string A
    // is already  sorted
    if (index == -1) {
        return false;
    }
  
    int j;
  
    // Finding first changed character
    // from  last of string A
    for (int i = 0; i < A.length(); i++) {
        if (A[i] == temp[index])
            j = i;
    }
  
    // Swap the two characters
    swap(A[index], A[j]);
  
    // Condition if string A
    // is smaller   than B
    if (A < B) {
        return true;
    }
  
    else {
        return false;
    }
}
  
// Drive Code
int main()
{
    string A = "AGAIN";
    string B = "ACTION";
  
    if (IsLexicographicallySmaller(A, B)) {
        cout << "Yes"
             << "\n";
    }
    else {
        cout << "No"
             << "\n";
    }
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program check whether is
// it possible to make String A
// lexicographically smaller than String B
import java.util.*;
  
class GFG{
      
      
    // Swap function
    static String swap(String str, int i, int j)
    {
        char[] tempArr = str.toCharArray();
        char temp = tempArr[i];
        tempArr[i] = tempArr[j];
        tempArr[j] = temp;
        return String.valueOf(tempArr);
    }
      
    // Function that finds whether is
    // it possible to make String A
    // lexicographically smaller than String B
    static boolean IsLexicographicallySmaller(String A, String B)
    {
        // Condition if String A
        // is already smaller than B
        if (A.compareTo(B) < 0) {
            return true;
        }
        String temp = A;
        char p[] = temp.toCharArray();
  
        // Sorting temp String
        Arrays.sort(p);
        temp=String.valueOf(p);
        int index = -1;
      
        for (int i = 0; i < A.length(); i++) {
  
            // Condition for first changed
            // character of String A and temp
            if (A.charAt(i) != temp.charAt(i)) {
                index = i;
                break;
            }
        }
      
        // Condition if String A
        // is already sorted
        if (index == -1) {
            return false;
        }
      
        int j = 0;
      
        // Finding first changed character
        // from last of String A
        for (int i = 0; i < A.length(); i++) {
            if (A.charAt(i) == temp.charAt(index))
                j = i;
        }
      
        // Swap the two characters
        A = swap(A, index, j);
      
        // Condition if String A
        // is smaller than B
        if (A.compareTo(B) < 0) {
            return true;
        }
      
        else {
            return false;
        }
    }
      
    // Drive Code
    public static void main(String args[])
    {
        String A = "AGAIN";
        String B = "ACTION";
      
        if (IsLexicographicallySmaller(A, B)) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
      
    }
}
  
// This code is contributed by AbhiThakur

chevron_right


Output:

Yes

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : abhaysingh290895

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.