Lexicographic smallest permutation of a String containing the second String as a Substring
Given two strings str1 and str2, the task is to find the lexicographic smallest permutation of str1 that contains str2 as a substring.
Note: Assume that the solution always exists.
Example:
Input: str1 = “abab”, str2 = “ab”
Output: “aabb”
Explanation: The Lexicographically smallest permutation of string str1 is “aabb”, Since “aabb” contains the string “ab” as a substring, therefore, “aabb” is the required answer.
Input: str1 = “geeksforgeeks”, str2 = “for”
Output: “eeeeforggkkss”
Approach: This problem can be solved using the concept of the Frequency Counting technique. Follow the steps below to solve this problem.
- Store the frequency of all the characters of the strings str1 and str2.
- Initialize the resultant string with the substring.
- Subtract the frequency of the second string from the frequency of the first string
- Now, append the remaining characters which are lexicographically smaller than or equal to the first character of the substring before the substring in the resultant string.
- Append the remaining characters in the lexicographical order behind the substring in the resultant string.
- Finally, print the resultant string.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
string findSmallestString(string str1,
string str2)
{
int freq1[26], freq2[26];
memset (freq1, 0, sizeof freq1);
memset (freq2, 0, sizeof freq2);
int n1 = str1.length();
int n2 = str2.length();
for ( int i = 0; i < n1; ++i) {
freq1[str1[i] - 'a' ]++;
}
for ( int i = 0; i < n2; ++i) {
freq2[str2[i] - 'a' ]++;
}
for ( int i = 0; i < 26; ++i) {
freq1[i] -= freq2[i];
}
string res = "" ;
int minIndex = str2[0] - 'a' ;
for ( int i = 0; i < 26; ++i) {
for ( int j = 0; j < freq1[i]; ++j) {
res += ( char )(i + 'a' );
}
if (i == minIndex) {
res += str2;
}
}
return res;
}
int main()
{
string str1 = "geeksforgeeksfor" ;
string str2 = "for" ;
cout << findSmallestString(str1, str2);
}
|
Java
import java.util.*;
class GFG{
static String findSmallestString(String str1,
String str2)
{
int []freq1 = new int [ 26 ];
int []freq2 = new int [ 26 ];
Arrays.fill(freq1, 0 );
Arrays.fill(freq2, 0 );
int n1 = str1.length();
int n2 = str2.length();
for ( int i = 0 ; i < n1; ++i)
{
freq1[str1.charAt(i) - 'a' ]++;
}
for ( int i = 0 ; i < n2; ++i)
{
freq2[str2.charAt(i) - 'a' ]++;
}
for ( int i = 0 ; i < 26 ; ++i)
{
freq1[i] -= freq2[i];
}
String res = "" ;
int minIndex = str2.charAt( 0 ) - 'a' ;
for ( int i = 0 ; i < 26 ; ++i)
{
for ( int j = 0 ; j < freq1[i]; ++j)
{
res += ( char )(i + 'a' );
}
if (i == minIndex)
{
res += str2;
}
}
return res;
}
public static void main(String[] args)
{
String str1 = "geeksforgeeksfor" ;
String str2 = "for" ;
System.out.print(findSmallestString(str1, str2));
}
}
|
Python3
def findSmallestString(str1, str2):
freq1 = [ 0 ] * 26
freq2 = [ 0 ] * 26
n1 = len (str1)
n2 = len (str2)
for i in range (n1):
freq1[ ord (str1[i]) - ord ( 'a' )] + = 1
for i in range (n2):
freq2[ ord (str2[i]) - ord ( 'a' )] + = 1
for i in range ( 26 ):
freq1[i] - = freq2[i]
res = ""
minIndex = ord (str2[ 0 ]) - ord ( 'a' )
for i in range ( 26 ):
for j in range (freq1[i]):
res + = chr (i + ord ( 'a' ))
if i = = minIndex:
res + = str2
return res
str1 = "geeksforgeeksfor"
str2 = "for"
print (findSmallestString(str1, str2))
|
C#
using System;
class GFG{
static String findSmallestString(String str1,
String str2)
{
int [] freq1 = new int [26];
int [] freq2 = new int [26];
int n1 = str1.Length;
int n2 = str2.Length;
for ( int i = 0; i < n1; ++i)
{
freq1[str1[i] - 'a' ]++;
}
for ( int i = 0; i < n2; ++i)
{
freq2[str2[i] - 'a' ]++;
}
for ( int i = 0; i < 26; ++i)
{
freq1[i] -= freq2[i];
}
String res = "" ;
int minIndex = str2[0] - 'a' ;
for ( int i = 0; i < 26; ++i)
{
for ( int j = 0; j < freq1[i]; ++j)
{
res += ( char )(i + 'a' );
}
if (i == minIndex)
{
res += str2;
}
}
return res;
}
public static void Main(String[] args)
{
String str1 = "geeksforgeeksfor" ;
String str2 = "for" ;
Console.Write(findSmallestString(str1, str2));
}
}
|
Javascript
<script>
function findSmallestString(str1, str2)
{
let freq1 = Array.from({length: 26}, (_, i) => 0);
let freq2 = Array.from({length: 26}, (_, i) => 0);
let n1 = str1.length;
let n2 = str2.length;
for (let i = 0; i < n1; ++i)
{
freq1[str1[i].charCodeAt() - 'a' .charCodeAt()]++;
}
for (let i = 0; i < n2; ++i)
{
freq2[str2[i].charCodeAt() - 'a' .charCodeAt()]++;
}
for (let i = 0; i < 26; ++i)
{
freq1[i] -= freq2[i];
}
let res = "" ;
let minIndex = str2[0].charCodeAt() - 'a' .charCodeAt();
for (let i = 0; i < 26; ++i)
{
for (let j = 0; j < freq1[i]; ++j)
{
res += String.fromCharCode(i + 'a' .charCodeAt());
}
if (i == minIndex)
{
res += str2;
}
}
return res;
}
let str1 = "geeksforgeeksfor" ;
let str2 = "for" ;
document.write(findSmallestString(str1, str2));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
07 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...