Lexicographically smaller string by swapping at most one character pair
Last Updated :
09 Sep, 2022
Given two strings A and B of all uppercase letters, 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:
- Sort string A.
- We can find the first position where A and sorted(A) doesn’t match.
- We then find the letter that should be in that position and swap it with the letter in the sorted(A).
- If there are multiple choices, it is better to take the one that occurs last, since it makes the resulting string smallest.
- Now, compare string A and string B.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void swap( char & x, char & y)
{
char temp = x;
x = y;
y = temp;
}
bool IsLexicographicallySmaller(
string A, string B)
{
if (A < B) {
return true ;
}
string temp = A;
sort(temp.begin(), temp.end());
int index = -1;
for ( int i = 0; i < A.length(); i++) {
if (A[i] != temp[i]) {
index = i;
break ;
}
}
if (index == -1) {
return false ;
}
int j;
for ( int i = 0; i < A.length(); i++) {
if (A[i] == temp[index])
j = i;
}
swap(A[index], A[j]);
if (A < B) {
return true ;
}
else {
return false ;
}
}
int main()
{
string A = "AGAIN" ;
string B = "ACTION" ;
if (IsLexicographicallySmaller(A, B)) {
cout << "Yes"
<< "\n" ;
}
else {
cout << "No"
<< "\n" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
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);
}
static boolean IsLexicographicallySmaller(String A, String B)
{
if (A.compareTo(B) < 0 ) {
return true ;
}
String temp = A;
char p[] = temp.toCharArray();
Arrays.sort(p);
temp=String.valueOf(p);
int index = - 1 ;
for ( int i = 0 ; i < A.length(); i++) {
if (A.charAt(i) != temp.charAt(i)) {
index = i;
break ;
}
}
if (index == - 1 ) {
return false ;
}
int j = 0 ;
for ( int i = 0 ; i < A.length(); i++) {
if (A.charAt(i) == temp.charAt(index))
j = i;
}
A = swap(A, index, j);
if (A.compareTo(B) < 0 ) {
return true ;
}
else {
return false ;
}
}
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" );
}
}
}
|
Python3
def IsLexicographicallySmaller(A, B):
if (A < B):
return True
temp = A
temp = ''.join( sorted (temp))
index = - 1
for i in range ( len (A)):
if (A[i] ! = temp[i]):
index = i
break
if (index = = - 1 ):
return False
j = 0
for i in range ( len (A)):
if (A[i] = = temp[index]):
j = i
A = list (A)
A[index], A[j] = A[j], A[index]
A = ''.join(A)
if (A < B):
return True
else :
return False
A = "AGAIN"
B = "ACTION"
if (IsLexicographicallySmaller(A, B)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
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 new string (tempArr);
}
static bool IsLexicographicallySmaller( string A,
string B)
{
if (A.CompareTo(B) < 0)
{
return true ;
}
string temp = A;
char []p = temp.ToCharArray();
Array.Sort(p);
temp= new string (p);
int index = -1;
for ( int i = 0; i < A.Length; i++)
{
if (A[i] != temp[i])
{
index = i;
break ;
}
}
if (index == -1)
{
return false ;
}
int j = 0;
for ( int i = 0; i < A.Length; i++)
{
if (A[i] == temp[index])
j = i;
}
A = swap(A, index, j);
if (A.CompareTo(B) < 0)
{
return true ;
}
else
{
return false ;
}
}
public static void Main( string []args)
{
string A = "AGAIN" ;
string B = "ACTION" ;
if (IsLexicographicallySmaller(A, B))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function swap(str,i,j)
{
let tempArr = str.split( "" );
let temp = tempArr[i];
tempArr[i] = tempArr[j];
tempArr[j] = temp;
return (tempArr).join( "" );
}
function IsLexicographicallySmaller(A, B)
{
if (A < (B) ) {
return true ;
}
let temp = A;
let p = temp.split( "" );
p.sort();
temp=(p).join( "" );
let index = -1;
for (let i = 0; i < A.length; i++) {
if (A[i] != temp[i]) {
index = i;
break ;
}
}
if (index == -1) {
return false ;
}
let j = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] == temp[index])
j = i;
}
A = swap(A, index, j);
if (A < (B) ) {
return true ;
}
else {
return false ;
}
}
let A = "AGAIN" ;
let B = "ACTION" ;
if (IsLexicographicallySmaller(A, B)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(N log N), for sorting the given string.
Auxiliary Space: O(N), for storing the given string in extra variable temp.
Share your thoughts in the comments
Please Login to comment...