Lexicographic smallest permutation of a String containing the second String as a Substring
Last Updated :
07 Mar, 2022
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++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to print the desired
// lexicographic smaller string
string findSmallestString(string str1,
string str2)
{
int freq1[26], freq2[26];
memset(freq1, 0, sizeof freq1);
memset(freq2, 0, sizeof freq2);
// Calculate length of the string
int n1 = str1.length();
int n2 = str2.length();
// Stores the frequencies of
// characters of string str1
for (int i = 0; i < n1; ++i) {
freq1[str1[i] - 'a']++;
}
// Stores the frequencies of
// characters of string str2
for (int i = 0; i < n2; ++i) {
freq2[str2[i] - 'a']++;
}
// Decrease the frequency of
// second string from that of
// of the first string
for (int i = 0; i < 26; ++i) {
freq1[i] -= freq2[i];
}
// To store the resultant string
string res = "";
// To find the index of first
// character of the string str2
int minIndex = str2[0] - 'a';
// Append the characters in
// lexicographical order
for (int i = 0; i < 26; ++i) {
// Append all the current
// character (i + 'a')
for (int j = 0; j < freq1[i]; ++j) {
res += (char)(i + 'a');
}
// If we reach first character
// of string str2 append str2
if (i == minIndex) {
res += str2;
}
}
// Return the resultant string
return res;
}
// Driver Code
int main()
{
string str1 = "geeksforgeeksfor";
string str2 = "for";
cout << findSmallestString(str1, str2);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to print the desired
// lexicographic smaller String
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);
// Calculate length of the String
int n1 = str1.length();
int n2 = str2.length();
// Stores the frequencies of
// characters of String str1
for(int i = 0; i < n1; ++i)
{
freq1[str1.charAt(i) - 'a']++;
}
// Stores the frequencies of
// characters of String str2
for(int i = 0; i < n2; ++i)
{
freq2[str2.charAt(i) - 'a']++;
}
// Decrease the frequency of
// second String from that of
// of the first String
for(int i = 0; i < 26; ++i)
{
freq1[i] -= freq2[i];
}
// To store the resultant String
String res = "";
// To find the index of first
// character of the String str2
int minIndex = str2.charAt(0) - 'a';
// Append the characters in
// lexicographical order
for(int i = 0; i < 26; ++i)
{
// Append all the current
// character (i + 'a')
for(int j = 0; j < freq1[i]; ++j)
{
res += (char)(i + 'a');
}
// If we reach first character
// of String str2 append str2
if (i == minIndex)
{
res += str2;
}
}
// Return the resultant String
return res;
}
// Driver Code
public static void main(String[] args)
{
String str1 = "geeksforgeeksfor";
String str2 = "for";
System.out.print(findSmallestString(str1, str2));
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program to implement
# the above approach
# Function to print the desired
# lexicographic smaller string
def findSmallestString(str1, str2):
freq1 = [0] * 26
freq2 = [0] * 26
# Calculate length of the string
n1 = len(str1)
n2 = len(str2)
# Stores the frequencies of
# characters of string str1
for i in range(n1):
freq1[ord(str1[i]) - ord('a')] += 1
# Stores the frequencies of
# characters of string str2
for i in range(n2):
freq2[ord(str2[i]) - ord('a')] += 1
# Decrease the frequency of
# second string from that of
# of the first string
for i in range(26):
freq1[i] -= freq2[i]
# To store the resultant string
res = ""
# To find the index of first
# character of the string str2
minIndex = ord(str2[0]) - ord('a')
# Append the characters in
# lexicographical order
for i in range(26):
# Append all the current
# character (i + 'a')
for j in range(freq1[i]):
res += chr(i+ ord('a'))
# If we reach first character
# of string str2 append str2
if i == minIndex:
res += str2
# Return the resultant string
return res
# Driver code
str1 = "geeksforgeeksfor"
str2 = "for"
print(findSmallestString(str1, str2))
# This code is contributed by Stuti Pathak
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to print the desired
// lexicographic smaller String
static String findSmallestString(String str1,
String str2)
{
int[] freq1 = new int[26];
int[] freq2 = new int[26];
// Calculate length of the String
int n1 = str1.Length;
int n2 = str2.Length;
// Stores the frequencies of
// characters of String str1
for (int i = 0; i < n1; ++i)
{
freq1[str1[i] - 'a']++;
}
// Stores the frequencies of
// characters of String str2
for (int i = 0; i < n2; ++i)
{
freq2[str2[i] - 'a']++;
}
// Decrease the frequency of
// second String from that of
// of the first String
for (int i = 0; i < 26; ++i)
{
freq1[i] -= freq2[i];
}
// To store the resultant String
String res = "";
// To find the index of first
// character of the String str2
int minIndex = str2[0] - 'a';
// Append the characters in
// lexicographical order
for (int i = 0; i < 26; ++i)
{
// Append all the current
// character (i + 'a')
for (int j = 0; j < freq1[i]; ++j)
{
res += (char)(i + 'a');
}
// If we reach first character
// of String str2 append str2
if (i == minIndex)
{
res += str2;
}
}
// Return the resultant String
return res;
}
// Driver Code
public static void Main(String[] args)
{
String str1 = "geeksforgeeksfor";
String str2 = "for";
Console.Write(findSmallestString(str1, str2));
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// Javascript program to implement
// the above approach
// Function to print the desired
// lexicographic smaller String
function findSmallestString(str1, str2)
{
let freq1 = Array.from({length: 26}, (_, i) => 0);
let freq2 = Array.from({length: 26}, (_, i) => 0);
// Calculate length of the String
let n1 = str1.length;
let n2 = str2.length;
// Stores the frequencies of
// characters of String str1
for (let i = 0; i < n1; ++i)
{
freq1[str1[i].charCodeAt() - 'a'.charCodeAt()]++;
}
// Stores the frequencies of
// characters of String str2
for (let i = 0; i < n2; ++i)
{
freq2[str2[i].charCodeAt() - 'a'.charCodeAt()]++;
}
// Decrease the frequency of
// second String from that of
// of the first String
for (let i = 0; i < 26; ++i)
{
freq1[i] -= freq2[i];
}
// To store the resultant String
let res = "";
// To find the index of first
// character of the String str2
let minIndex = str2[0].charCodeAt() - 'a'.charCodeAt();
// Append the characters in
// lexicographical order
for (let i = 0; i < 26; ++i)
{
// Append all the current
// character (i + 'a')
for (let j = 0; j < freq1[i]; ++j)
{
res += String.fromCharCode(i + 'a'.charCodeAt());
}
// If we reach first character
// of String str2 append str2
if (i == minIndex)
{
res += str2;
}
}
// Return the resultant String
return res;
}
// Driver code
let str1 = "geeksforgeeksfor";
let str2 = "for";
document.write(findSmallestString(str1, str2));
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Lexicographically smallest permutation of a string with given subsequences Given a string consisting only of two lowercase characters x and y and two numbers p and q . The task is to print the lexicographically smallest permutation of the given string such that the count of subsequences of xy is p and of yx is q . If no such string exists, print "Impossible" (without quote
9 min read
Minimum size lexicographically smallest string which is not a substring of given string Given a string s, the task is to find the lexicographically smallest string of minimum characters that do not exist as a substring in S. Examples: Input: S = "aabacdefghijklmnopqrstuvwxyz"Output: adExplanation: All the single digit strings from [a-z] occur in the given string and in two character st
7 min read
Lexicographically smallest and largest anagrams of a string containing another string as its substring Given two strings S1 of size N and S2 of size M, the task is to find the lexicographically smallest and the largest anagrams of S1 such that it contains the string S2 as a substring. Examples: Input: S1 = "hheftaabzzdr", S2 = "earth" Output: abdearthfhzz, zzhfearthdba Explanation: The smallest anagr
13 min read
Lexicographically smallest string formed by reversing Substrings of string S exactly K times Given a string S and an integer K, the task is to find the lexicographically smallest string possible after reversing any substring of any length exactly K times. Examples: Input: S = "fgazcbdfge", K = 3Output: abcdgfzfgeExplanation: After 1st operation: S = "agfzcbdfge", in S select S[0 - 2] = "fga
11 min read
Lexicographically shortest string of length at most K which is not a substring of given String Given a string S, the task is to find the lexicographically shortest string of length less than or equal to K which is not a substring of the given string. If not possible, print -1. Examples: Input: S = zxabcehgf, K = 2Output: dExplanation: Lexicographically, the shortest string which is not a subs
9 min read
Find smallest Substring to be rearranged to make String lexicographically sorted Given a string S, the task is to find out the length of the smallest substring of S that needs to be rearranged so that all the characters of the string S are in lexicographical order. Examples: Input: S = "aabbace"Output: 3Explanation: Rearranging "bba" to be "abb".S becomes "aaabbce" which is in l
4 min read