Check if any permutation of a given string is lexicographically larger than the other given string
Last Updated :
07 Jun, 2023
Given two strings str1 and str2 of same length N, the task is to check if there exists any permutation possible in any of the given strings, such that every character of one string is greater or equal to every character of the other string, at corresponding indices. Return true if permutation exists otherwise false.
Example:
Input: str1 = “adb”, str2 = “cda”
Output: true
Explanation: After permutation str1 = “abd” and str2 = “acd”, so every character from str2 is greater or equals to every character from s1.
Input: str1 = “gfg”, str2 = “agd”
Output: true
Approach: The above problem can be solved by sorting both the strings and then lexicographically comparing them.
Follow the below steps to understand how:
Below is the implementation of above approach:
C++
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
bool checkGreaterOrNot(string str1,
string str2)
{
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());
bool flag = true ;
for ( int i = 0; i < str1.length(); i++) {
if (str1[i] < str2[i]) {
flag = false ;
break ;
}
}
if (flag)
return true ;
flag = true ;
for ( int i = 0; i < str2.length(); i++){
if (str1[i] > str2[i]) {
return false ;
}
}
return true ;
}
int main()
{
string str1 = "adb" ;
string str2 = "cda" ;
bool ans =
checkGreaterOrNot(str1, str2);
if (ans) {
cout << "true" ;
}
else {
cout << "false" ;
}
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static boolean
checkGreaterOrNot(String str1,
String str2)
{
char [] arr1 = str1.toCharArray();
Arrays.sort(arr1);
char [] arr2 = str2.toCharArray();
Arrays.sort(arr2);
boolean flag = true ;
for ( int i = 0 ; i < arr1.length; i++) {
if (arr1[i] < arr2[i]) {
flag = false ;
break ;
}
}
if (flag)
return true ;
flag = true ;
for ( int i = 0 ; i < arr2.length; i++) {
if (arr1[i] > arr2[i]) {
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
String str1 = "adb" ;
String str2 = "cda" ;
boolean ans = checkGreaterOrNot(str1, str2);
System.out.println(ans);
}
}
|
Python3
def checkGreaterOrNot(str1, str2):
str1 = sorted (str1)
str1 = "".join(str1)
str2 = sorted (str2)
str2 = "".join(str2)
flag = True
for i in range ( len (str1)):
if (str1[i] < str2[i]):
flag = False
break
if (flag):
return True
flag = True
for i in range ( len (str2)):
if (str1[i] > str2[i]):
return False
return True
if __name__ = = '__main__' :
str1 = "adb"
str2 = "cda"
ans = checkGreaterOrNot(str1, str2)
if (ans):
print ( "true" )
else :
print ( "false" )
|
C#
using System;
class GFG {
public static bool checkGreaterOrNot( string str1,
string str2)
{
char [] arr1 = str1.ToCharArray();
Array.Sort(arr1);
char [] arr2 = str2.ToCharArray();
Array.Sort(arr2);
bool flag = true ;
for ( int i = 0; i < arr1.Length; i++) {
if (arr1[i] < arr2[i]) {
flag = false ;
break ;
}
}
if (flag)
return true ;
flag = true ;
for ( int i = 0; i < arr2.Length; i++) {
if (arr1[i] > arr2[i]) {
return false ;
}
}
return true ;
}
public static void Main( string [] args)
{
string str1 = "adb" ;
string str2 = "cda" ;
bool ans = checkGreaterOrNot(str1, str2);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function checkGreaterOrNot(str1,
str2)
{
str1.sort( function (a, b) { return a.charCodeAt(0) - b.charCodeAt(0); });
str2.sort( function (a, b) { return a.charCodeAt(0) - b.charCodeAt(0); });
let flag = true ;
for (let i = 0; i < str1.length; i++) {
if (str1[i].charCodeAt(0) > str2[i].charCodeAt(0)) {
flag = false ;
break ;
}
}
if (flag)
return true ;
flag = true ;
for (let i = 0; i < str2.length; i++) {
if (str1[i].charCodeAt(0) > str2[i].charCodeAt(0)) {
return false ;
}
}
return true ;
}
let str1 = [ 'a' , 'd' , 'b' ];
let str2 = [ 'c' , 'd' , 'a' ]
let ans =
checkGreaterOrNot(str1, str2);
if (ans) {
document.write( "true" );
}
else {
document.write( "false" );
}
</script>
|
Time Complexity: O(n*log n)
Auxiliary Space: O(n)
Approach 2: The above approach can be optimized using frequency map for given strings.
- Make frequency map for both the given strings
- Create variables count1 and count2 to indicate cumulative frequency of respective strings
- Iterate through frequency map and check if value for any string is greater than the other or not.
- If yes, print true. Otherwise print false.
Below is the implementation of above approach:
C++
#include <iostream>
#include <string>
using namespace std;
bool checkGreaterOrNot(string str1,
string str2)
{
int arr1[26] = { 0 };
int arr2[26] = { 0 };
for ( int i = 0;
i < str1.length(); i++) {
arr1[str1[i] - 'a' ]++;
}
for ( int i = 0;
i < str2.length(); i++) {
arr2[str2[i] - 'a' ]++;
}
bool str1IsSmaller = false ,
str2IsSmaller = false ;
int count1 = 0, count2 = 0;
for ( int i = 0; i < 26; i++) {
count1 += arr1[i];
count2 += arr2[i];
if (count1 > count2) {
if (str2IsSmaller)
return false ;
str1IsSmaller = true ;
}
if (count1 < count2) {
if (str1IsSmaller)
return false ;
str2IsSmaller = true ;
}
}
return true ;
}
int main()
{
string str1 = "geeks" ;
string str2 = "peeks" ;
bool ans =
checkGreaterOrNot(str1, str2);
if (ans) {
cout << "true" ;
}
else {
cout << "false" ;
}
}
|
Java
import java.util.*;
class GFG {
public static boolean checkGreaterOrNot(
String str1, String str2)
{
int [] freq1 = new int [ 26 ];
int [] freq2 = new int [ 26 ];
for ( int i = 0 ;
i < str1.length(); i++) {
freq1[str1.charAt(i) - 'a' ]++;
}
for ( int i = 0 ;
i < str2.length(); i++) {
freq2[str2.charAt(i) - 'a' ]++;
}
boolean str1IsSmaller = false ;
boolean str2IsSmaller = false ;
int count1 = 0 , count2 = 0 ;
for ( int i = 0 ; i < 26 ; i++) {
count1 += freq1[i];
count2 += freq2[i];
if (count1 > count2) {
if (str2IsSmaller)
return false ;
str1IsSmaller = true ;
}
else if (count2 > count1) {
if (str1IsSmaller)
return false ;
str2IsSmaller = true ;
}
}
return true ;
}
public static void main(String[] args)
{
String str1 = "geeks" ;
String str2 = "peeks" ;
boolean ans = checkGreaterOrNot(str1, str2);
System.out.println(ans);
}
}
|
Python3
def checkGreaterOrNot(str1, str2):
arr1 = [ 0 for x in range ( 26 )]
arr2 = [ 0 for x in range ( 26 )]
for val in str1:
arr1[ ord (val) - 97 ] + = 1
for val in str2:
arr2[ ord (val) - 97 ] + = 1
str1IsSmaller = False
str2IsSmaller = False
count1 = 0
count2 = 0
for i in range ( 0 , 26 ):
count1 + = arr1[i]
count2 + = arr2[i]
if (count1 > count2):
if str2IsSmaller = = True :
return False
str1IsSmaller = True
if (count1 < count2):
if str1IsSmaller = = True :
return False
str2IsSmaller = True
return True
str1 = "geeks"
str2 = "peeks"
ans = checkGreaterOrNot(str1, str2)
if ans = = True :
print ( "true" )
else :
print ( "false" )
|
C#
using System;
class GFG {
public static bool checkGreaterOrNot(
string str1, string str2)
{
int [] freq1 = new int [26];
int [] freq2 = new int [26];
for ( int i = 0;
i < str1.Length; i++) {
freq1[str1[(i)] - 'a' ]++;
}
for ( int i = 0;
i < str2.Length; i++) {
freq2[str2[(i)] - 'a' ]++;
}
bool str1IsSmaller = false ;
bool str2IsSmaller = false ;
int count1 = 0, count2 = 0;
for ( int i = 0; i < 26; i++) {
count1 += freq1[i];
count2 += freq2[i];
if (count1 > count2) {
if (str2IsSmaller)
return false ;
str1IsSmaller = true ;
}
else if (count2 > count1) {
if (str1IsSmaller)
return false ;
str2IsSmaller = true ;
}
}
return true ;
}
public static void Main()
{
string str1 = "geeks" ;
string str2 = "peeks" ;
bool ans = checkGreaterOrNot(str1, str2);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function checkGreaterOrNot(str1, str2)
{
var arr1 = Array(26).fill(0);
var arr2 = Array(26).fill(0);
for ( var i = 0;
i < str1.length; i++) {
arr1[str1[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
for ( var i = 0;
i < str2.length; i++) {
arr2[str2[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
var str1IsSmaller = false ,
str2IsSmaller = false ;
var count1 = 0, count2 = 0;
for ( var i = 0; i < 26; i++) {
count1 += arr1[i];
count2 += arr2[i];
if (count1 > count2) {
if (str2IsSmaller)
return false ;
str1IsSmaller = true ;
}
if (count1 < count2) {
if (str1IsSmaller)
return false ;
str2IsSmaller = true ;
}
}
return true ;
}
var str1 = "geeks" ;
var str2 = "peeks" ;
var ans =
checkGreaterOrNot(str1, str2);
if (ans) {
document.write( "true" );
}
else {
document.write( "false" );
}
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...