Lexicographically smallest String formed by extracting single character
Last Updated :
20 Dec, 2023
Given a string array S, the task is to find the lexicographically smallest string that can be formed by extracting a single character in each string from the string array S.
Example:
Input: S = [“xy”, “fd”]
Output: “dx”
Explanation: The possible strings formed by extracting a single character from each string are [“xf”, “fx”, “xd”, “dx”, “yf”, “fy”, “yd”, “dy”]. The lexicographically smallest string obtained is “dx”.
Approach: To solve the problem follow the below idea:
The idea is to extract the smallest character in each string and then rearrange them to get the lexicographically smallest string.
Steps to solve the problem:
- Create a frequency array to store the frequency of each smallest character in the array of strings.
- Iterate over each string and find out the smallest character and add it to the frequency array.
- Traverse the frequency array and append the characters to the final string.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string LexicographicallySmallest(vector<string>& words)
{
vector< int > frequency(26, 0);
for (string s : words) {
int mini = 27;
char best_char = 'z' ;
for ( char c : s) {
if (mini > (c - 'a' )) {
mini = c - 'a' ;
best_char = c;
}
}
frequency[best_char - 'a' ] += 1;
}
string result = "";
for ( int i = 0; i < 26; i++) {
while (frequency[i]--) {
result += char (i + 'a' );
}
}
return result;
}
int main()
{
vector<string> words = { "xy", "fd" };
cout << LexicographicallySmallest(words);
return 0;
}
|
Java
import java.util.*;
public class LexicographicallySmallest {
public static String
lexicographicallySmallest(String[] words)
{
int [] frequency = new int [ 26 ];
for (String s : words) {
int mini = 27 ;
char bestChar = 'z' ;
for ( char c : s.toCharArray()) {
if (mini > (c - 'a' )) {
mini = c - 'a' ;
bestChar = c;
}
}
frequency[bestChar - 'a' ] += 1 ;
}
StringBuilder result = new StringBuilder();
for ( int i = 0 ; i < 26 ; i++) {
while (frequency[i] > 0 ) {
result.append(( char )(i + 'a' ));
frequency[i] -= 1 ;
}
}
return result.toString();
}
public static void main(String[] args)
{
String[] words = { "xy" , "fd" };
System.out.println(
lexicographicallySmallest(words));
}
}
|
Python3
def lexicographically_smallest(words):
frequency = [ 0 ] * 26
for s in words:
mini = 27
best_char = 'z'
for c in s:
if mini > ( ord (c) - ord ( 'a' )):
mini = ord (c) - ord ( 'a' )
best_char = c
frequency[ ord (best_char) - ord ( 'a' )] + = 1
result = ""
for i in range ( 26 ):
while frequency[i] > 0 :
result + = chr (i + ord ( 'a' ))
frequency[i] - = 1
return result
if __name__ = = "__main__" :
words = [ "xy" , "fd" ]
print (lexicographically_smallest(words))
|
C#
using System;
using System.Text;
public class GFG
{
public static string LexicographicallySmallest( string [] words)
{
int [] frequency = new int [26];
foreach ( string s in words)
{
int mini = 27;
char bestChar = 'z' ;
foreach ( char c in s.ToCharArray())
{
if (mini > (c - 'a' ))
{
mini = c - 'a' ;
bestChar = c;
}
}
frequency[bestChar - 'a' ] += 1;
}
StringBuilder result = new StringBuilder();
for ( int i = 0; i < 26; i++)
{
while (frequency[i] > 0)
{
result.Append(( char )(i + 'a' ));
frequency[i] -= 1;
}
}
return result.ToString();
}
public static void Main( string [] args)
{
string [] words = { "xy" , "fd" };
Console.WriteLine(LexicographicallySmallest(words));
}
}
|
Javascript
function LexicographicallySmallest(words) {
const frequency = new Array(26).fill(0);
for (const s of words) {
let mini = 27;
let best_char = 'z' ;
for (const c of s) {
if (mini > c.charCodeAt(0) - 'a' .charCodeAt(0)) {
mini = c.charCodeAt(0) - 'a' .charCodeAt(0);
best_char = c;
}
}
frequency[best_char.charCodeAt(0) - 'a' .charCodeAt(0)] += 1;
}
let result = '' ;
for (let i = 0; i < 26; i++) {
while (frequency[i]--) {
result += String.fromCharCode(i + 'a' .charCodeAt(0));
}
}
return result;
}
const words = [ "xy" , "fd" ];
console.log(LexicographicallySmallest(words));
|
Time Complexity: O(number of words * length of each word)
Auxiliary Space: O(1) (Considering frequency array space as constant)
Share your thoughts in the comments
Please Login to comment...