Given a string str of N characters, the task is to find the lexicographically smallest string that can be formed by concatenating any prefix and its mirrored form.
Examples:
Input: str = “geeksforgeeks”
Output: geeeeg
Explanation: The lexicographically smallest string can be formed with the prefix “gee” as “gee” + “eeg”.Input: str = “abcd”
Output: aa
Approach: The given problem can be solved using a Greedy Approach. The idea is to choose the largest prefix having their ASCII values in decreasing order. So, traverse through the string str and store the largest prefix having the value of its ASCII characters in decreasing order into a string prefix. The concatenation of prefix with its reverse is the required answer.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find lexicographically // smallest string formed by concatenating // any prefix and its mirrored form string lexicographicallySmallest(string str) { // Stores the largest prefix
// with character in decreasing
// order of ascii value
string prefix = "" ;
// Initialize prefix string
prefix += str[0];
// Loop to traverse the string
for ( int i = 1; i < str.length(); i++) {
// If current character is
// in decreasing order
if (str[i] < prefix.back()) {
prefix += str[i];
}
else if (str[i] == prefix.back()
&& prefix.size() > 1) {
prefix += str[i];
}
else {
break ;
}
}
// Stores the reversed prefix string
string rev = prefix;
reverse(rev.begin(), rev.end());
// Return Answer
return prefix + rev;
} // Driver code int main()
{ string str = "geeksforgeeks" ;
cout << lexicographicallySmallest(str);
return 0;
} |
// Java program for the above approach class GFG
{ static String reverse(String str) {
char [] chars = str.toCharArray();
for ( int i = 0 , j = str.length() - 1 ; i < j; i++, j--) {
char c = chars[i];
chars[i] = chars[j];
chars[j] = c;
}
return new String(chars);
}
// Function to find lexicographically
// smallest string formed by concatenating
// any prefix and its mirrored form
static String lexicographicallySmallest(String str)
{
// Stores the largest prefix
// with character in decreasing
// order of ascii value
String prefix = "" ;
// Initialize prefix string
prefix += str.charAt( 0 );
// Loop to traverse the string
for ( int i = 1 ; i < str.length(); i++) {
// If current character is
// in decreasing order
if (str.charAt(i) < prefix.charAt(prefix.length() - 1 )) {
prefix += str.charAt(i);
}
else if (str.charAt(i) == prefix.charAt(prefix.length() - 1 ) && prefix.length() > 1 ) {
prefix += str.charAt(i);
}
else {
break ;
}
}
// Stores the reversed prefix string
String rev = reverse(prefix);
// Return Answer
return prefix + rev;
}
// Driver code
public static void main(String args[])
{
String str = "geeksforgeeks" ;
System.out.println(lexicographicallySmallest(str));
}
} // This code is contributed by gfgking |
# Python 3 program for the above approach # Function to find lexicographically # smallest string formed by concatenating # any prefix and its mirrored form def lexicographicallySmallest(st):
# Stores the largest prefix
# with character in decreasing
# order of ascii value
prefix = ""
# Initialize prefix string
prefix + = st[ 0 ]
# Loop to traverse the string
for i in range ( 1 , len (st)):
# If current character is
# in decreasing order
if (st[i] < prefix[ len (prefix) - 1 ]):
prefix + = st[i]
elif (st[i] = = prefix[ len (prefix) - 1 ]
and len (prefix) > 1 ):
prefix + = st[i]
else :
break
# Stores the reversed prefix string
rev = prefix
rev = list (rev)
rev.reverse()
rev = ''.join(rev)
# Return Answer
return prefix + rev
# Driver code if __name__ = = "__main__" :
st = "geeksforgeeks"
print (lexicographicallySmallest(st))
# This code is contributed by ukasp.
|
// C# program for the above approach using System;
class GFG
{ static string reverse( string str) {
char [] chars = str.ToCharArray();
for ( int i = 0, j = str.Length - 1; i < j; i++, j--) {
char c = chars[i];
chars[i] = chars[j];
chars[j] = c;
}
return new string (chars);
}
// Function to find lexicographically
// smallest string formed by concatenating
// any prefix and its mirrored form
static string lexicographicallySmallest( string str)
{
// Stores the largest prefix
// with character in decreasing
// order of ascii value
string prefix = "" ;
// Initialize prefix string
prefix += str[0];
// Loop to traverse the string
for ( int i = 1; i < str.Length; i++) {
// If current character is
// in decreasing order
if (str[i] < prefix[prefix.Length - 1]) {
prefix += str[i];
}
else if (str[i] == prefix[prefix.Length - 1]
&& prefix.Length > 1) {
prefix += str[i];
}
else {
break ;
}
}
// Stores the reversed prefix string
string rev = reverse(prefix);
// Return Answer
return prefix + rev;
}
// Driver code
public static void Main()
{
string str = "geeksforgeeks" ;
Console.Write(lexicographicallySmallest(str));
}
} // This code is contributed by Samim Hossain Mondal. |
<script> // JavaScript program for the above approach
// Function to find lexicographically
// smallest string formed by concatenating
// any prefix and its mirrored form
const lexicographicallySmallest = (str) => {
// Stores the largest prefix
// with character in decreasing
// order of ascii value
let prefix = "" ;
// Initialize prefix string
prefix += str[0];
// Loop to traverse the string
for (let i = 1; i < str.length; i++) {
// If current character is
// in decreasing order
if (str[i] < prefix[prefix.length - 1]) {
prefix += str[i];
}
else if (str[i] == prefix[prefix.length - 1]
&& prefix.length > 1) {
prefix += str[i];
}
else {
break ;
}
}
// Stores the reversed prefix string
let rev = [...prefix];
rev.reverse();
// Return Answer
return prefix + rev.join( "" );
}
// Driver code
let str = "geeksforgeeks" ;
document.write(lexicographicallySmallest(str));
// This code is contributed by rakeshsahni </script> |
geeeeg
Time Complexity: O(N)
Auxiliary Space: O(N)