Lexicographically smallest string formed by concatenating any prefix and its mirrored form
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++
#include <bits/stdc++.h>
using namespace std;
string lexicographicallySmallest(string str)
{
string prefix = "" ;
prefix += str[0];
for ( int i = 1; i < str.length(); i++) {
if (str[i] < prefix.back()) {
prefix += str[i];
}
else if (str[i] == prefix.back()
&& prefix.size() > 1) {
prefix += str[i];
}
else {
break ;
}
}
string rev = prefix;
reverse(rev.begin(), rev.end());
return prefix + rev;
}
int main()
{
string str = "geeksforgeeks" ;
cout << lexicographicallySmallest(str);
return 0;
}
|
Java
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);
}
static String lexicographicallySmallest(String str)
{
String prefix = "" ;
prefix += str.charAt( 0 );
for ( int i = 1 ; i < str.length(); i++) {
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 ;
}
}
String rev = reverse(prefix);
return prefix + rev;
}
public static void main(String args[])
{
String str = "geeksforgeeks" ;
System.out.println(lexicographicallySmallest(str));
}
}
|
Python3
def lexicographicallySmallest(st):
prefix = ""
prefix + = st[ 0 ]
for i in range ( 1 , len (st)):
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
rev = prefix
rev = list (rev)
rev.reverse()
rev = ''.join(rev)
return prefix + rev
if __name__ = = "__main__" :
st = "geeksforgeeks"
print (lexicographicallySmallest(st))
|
C#
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);
}
static string lexicographicallySmallest( string str)
{
string prefix = "" ;
prefix += str[0];
for ( int i = 1; i < str.Length; i++) {
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 ;
}
}
string rev = reverse(prefix);
return prefix + rev;
}
public static void Main()
{
string str = "geeksforgeeks" ;
Console.Write(lexicographicallySmallest(str));
}
}
|
Javascript
<script>
const lexicographicallySmallest = (str) => {
let prefix = "" ;
prefix += str[0];
for (let i = 1; i < str.length; i++) {
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 ;
}
}
let rev = [...prefix];
rev.reverse();
return prefix + rev.join( "" );
}
let str = "geeksforgeeks" ;
document.write(lexicographicallySmallest(str));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
09 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...