Lexicographically smallest string with given string as prefix
Last Updated :
07 Oct, 2021
Given an array arr[] consisting of N strings and a string S if size M, the task is to find the lexicographically smallest string consisting of the string S as the prefix. If there doesn’t exist any string starting with prefix S then print “-1”.
Examples:
Input: arr[] = {“apple”, “appe”, “apl”, “aapl”, “appax”}, S = “app”
Output: appax
Explanation:
The lexicographical order of the strings consisting of “app” as the substring is {“aapl”, “apl”, “appax”, “appe”, “apple”}. The smallest lexicographic string containing is “appax”.
Input: arr[] = {“can”, “man”, “va”}, S = “van”
Output: -1
Approach: The given problem can be solved by sorting the given array of strings arr[] such that all the strings starting with prefixes S occur consecutively. Now traverse the given array of strings arr[] and when the first string whose prefix matches with S then print that string and break out of the loop. Otherwise, print “-1”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool is_prefix(string temp, string str)
{
if (temp.length() < str.length())
return 0;
else {
for ( int i = 0;
i < str.length(); i++) {
if (str[i] != temp[i])
return 0;
}
return 1;
}
}
string lexicographicallyString(
string input[], int n, string str)
{
sort(input, input + n);
for ( int i = 0; i < n; i++) {
string temp = input[i];
if (is_prefix(temp, str)) {
return temp;
}
}
return "-1" ;
}
int main()
{
string arr[] = { "apple" , "appe" , "apl" ,
"aapl" , "appax" };
string S = "app" ;
int N = 5;
cout << lexicographicallyString(
arr, N, S);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static boolean is_prefix(String temp, String str)
{
if (temp.length() < str.length())
return false ;
else {
for ( int i = 0 ; i < str.length(); i++) {
if (str.charAt(i) != temp.charAt(i))
return false ;
}
return true ;
}
}
static String lexicographicallyString(String[] input,
int n, String str)
{
Arrays.sort(input);
for ( int i = 0 ; i < n; i++) {
String temp = input[i];
if (is_prefix(temp, str)) {
return temp;
}
}
return "-1" ;
}
public static void main(String args[])
{
String[] arr = { "apple" , "appe" , "apl" , "aapl" , "appax" };
String S = "app" ;
int N = 5 ;
System.out.println(
lexicographicallyString(arr, N, S));
}
}
|
Python3
def is_prefix(temp, str ):
if ( len (temp) < len ( str )):
return 0
else :
for i in range ( len ( str )):
if ( str [i] ! = temp[i]):
return 0
return 1
def lexicographicallyString( input , n, str ):
input .sort()
for i in range (n):
temp = input [i]
if (is_prefix(temp, str )):
return temp
return "-1"
if __name__ = = '__main__' :
arr = [ "apple" , "appe" , "apl" , "aapl" , "appax" ]
S = "app"
N = 5
print (lexicographicallyString(arr, N, S))
|
C#
using System;
class GFG {
static bool is_prefix( string temp, string str)
{
if (temp.Length < str.Length)
return false ;
else {
for ( int i = 0; i < str.Length; i++) {
if (str[i] != temp[i])
return false ;
}
return true ;
}
}
static string lexicographicallyString( string [] input,
int n, string str)
{
Array.Sort(input);
for ( int i = 0; i < n; i++) {
string temp = input[i];
if (is_prefix(temp, str)) {
return temp;
}
}
return "-1" ;
}
public static void Main()
{
string [] arr
= { "apple" , "appe" , "apl" , "aapl" , "appax" };
string S = "app" ;
int N = 5;
Console.WriteLine(
lexicographicallyString(arr, N, S));
}
}
|
Javascript
<script>
function is_prefix(temp, str)
{
if (temp.length < str.length)
return 0;
else {
for (let i = 0;
i < str.length; i++) {
if (str[i] != temp[i])
return 0;
}
return 1;
}
}
function lexicographicallyString(
input, n, str)
{
input = Array.from(input).sort();
for (let i = 0; i < n; i++) {
let temp = input[i];
if (is_prefix(temp, str)) {
return temp;
}
}
return "-1" ;
}
let arr = [ "apple" , "appe" , "apl" ,
"aapl" , "appax" ];
let S = "app" ;
let N = 5;
document.write(lexicographicallyString(
arr, N, S));
</script>
|
Time Complexity: O(M*K*N*log N), where K is the maximum length of the string in the array arr[].
Auxiliary Space: O(N)
Another Approach: The above approach can also be optimized by using the Trie Data Structure by inserting all the given strings in the Trie and then check for the first string that exists in the Trie having prefix S.
Time Complexity: O(M*N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...