Given two strings P and Q, the task is to generate a string S satisfying the following conditions:
- Find S such that P is rearranged and Q is a substring in it.
- All the characters before Q in S should be smaller than or equal to the first character in Q and in lexicographic order.
- The rest of the characters should be present after Q in lexicographic order
Note: All characters of Q are always present in P and length of Q is always less than or equal to the length of P.
Input : P = “geeksforgeeksfor” Q = “for”
Output : eeeefforggkkorss
The characters ‘e’ and ‘f’ are the only characters here which are less than or equal to ‘f’ (first character of Q).
So, before “for” the string is lexicographically equal to eeeef.
The rest of the characters in P are greater than ‘f’, so they are placed after “for” in lexicographic order.
Thus, after “for”, the string is ggkkorss.
Therefore the output is eeeefforggkkorss.
Input : P = “lexicographical” Q = “gap”
Output : accegaphiillorx
The string accegaphiillorx satisfies the above conditions for string P and Q.
Approach: The idea is to find the frequencies of all the characters in P and Q to solve the problem.
- Maintain an array of frequencies of all the alphabets in P and Q.
- After finding the frequencies, segregate the characters in P according to the first character in Q and add them to the resulting string.
- Return the resulting string at the end.
Below is the implementation of the above approach:
Time Complexity: O(N+M) where N and M are the respective lengths of P and Q.
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Generate a string from an array of alphanumeric strings based on given conditions
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- Construct the Cypher string based on the given conditions
- Sum of indices of Characters removed to obtain an Empty String based on given conditions
- Queries to search for an element in an array and modify the array based on given conditions
- Calculate weight of parenthesis based on the given conditions
- Maximum Sum possible by selecting X elements from a Matrix based on given conditions
- Maximum Count of pairs having equal Sum based on the given conditions
- Calculate the Square of Euclidean Distance Traveled based on given conditions
- Count pairs of strings that satisfy the given conditions
- Number of strings in two array satisfy the given conditions
- Count of Binary Strings possible as per given conditions
- Generate string with Hamming Distance as half of the hamming distance between strings A and B
- Generate a string which differs by only a single character from all given strings
- Generate two output strings depending upon occurrence of character in input string.
- Generate two output strings depending upon occurrence of character in input string in Python
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Change the given string according to the given conditions
- Sort an array of strings based on the given order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.