Lexicographically Smallest String formed by Concatenating any prefix and its Mirrored form


In this problem, we will find the lexicographically smallest string by concatenating the given string’s prefix and its reverse. We can find the lexicographically smallest prefix of the string and get the required string.

Problem statement – We have given a string alpha containing the alphabetical characters. We need to construct the lexicographically smallest string, which we can get by concatenating any string prefix with its reverse.

Sample examples

Input

alpha = "welcome";

Output

'wewe’

Explanation – The lexicographically smallest prefix is ‘we’. So, we have concatenated it with its mirror.

Input

alpha = "tutorialspoint"

Output

‘tt’

Explanation – The ‘tu’ is greater than ‘t’, so all other prefixes will also be greater than ‘tu’.

Input

alpha = "edcba";

Output

edcbaabcde

Explanation – All characters of the string are in decreasing order. So, we have taken the whole string as a prefix and merged it with its reverse.

Approach 1

In this approach, we will traverse the string until we get the prefix that contains the characters in decreasing order. After that, we will concatenate with its reverse to get the lexicographically smallest string.

Algorithm

Step 1 – Initialize the ‘preStr’ string variable with the first characters of the string to store the lexicographically smallest prefix.

Step 2 – Start iterating the string.

Step 3 – In the loop, if the character at the pth index is lexicographically smaller than the last character of the ‘preStr’ string, append the character to the ‘preStr’ string.

Step 4 – if the character is the same as the last character of the ‘preStr’ string, and the size of the ‘preStr’ string is greater than 1, append the character to the ‘preStr’.

Step 5 – In all other cases, break the loop.

Step 6 – Store the ‘preStr’ string in the ‘temp’ string variable and reveres it.

Step 7 – Return the resultant string after concatenating the ‘preStr’ and the ‘temp’ string.

Example

#include <bits/stdc++.h>
using namespace std;

string getString(string alpha) {
    // To store the prefix string
    string preStr = "";
    // Initialization
    preStr += alpha[0];
    // Iterate the string
    for (int p = 1; p < alpha.length(); p++) {
        // Get largest prefix string in decreasing order
        if (alpha[p] < preStr.back()) {
            preStr += alpha[p];
        } else if (alpha[p] == preStr.back() && preStr.size() > 1) {
            preStr += alpha[p];
        } else {
            break;
        }
    }
    // Do reverse of string
    string temp = preStr;
    reverse(temp.begin(), temp.end());
    // Final answer
    return preStr + temp;
}
int main() {
    string alpha = "welcome";
    cout << "The output string is - " << getString(alpha);
    return 0;
}

Output

The output string is - weew

Time complexity – O(N) to find the lexicographically smallest prefix.

Space complexity – O(N) to store the resultant string.

If we take any other prefix than the lexicographically smallest prefix, we can’t construct the lexicographically smallest string by concatenating the prefix and its reverse. Programmers may try to solve the problem in which we are required to construct the lexicographically smallest string by concatenating any substrings or subsequences of the given string.

Updated on: 25-Aug-2023

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements