以给定字符串为前缀的字典最小字符串
给定一个由N个字符串组成的数组arr[]和一个大小为M的字符串S ,任务是找到由字符串S作为前缀组成的字典上最小的字符串。如果不存在任何以前缀S开头的字符串,则打印“-1” 。
例子:
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
方法:给定的问题可以通过对给定的字符串数组arr[]进行排序来解决,使得所有以前缀S开头的字符串连续出现。现在遍历给定的字符串数组arr[] ,当第一个字符串的前缀与S匹配时,打印该字符串并跳出循环。否则,打印“-1” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the whether the
// string temp starts with str or not
bool is_prefix(string temp, string str)
{
// Base Case
if (temp.length() < str.length())
return 0;
else {
// Check for the corresponding
// characters in temp & str
for (int i = 0;
i < str.length(); i++) {
if (str[i] != temp[i])
return 0;
}
return 1;
}
}
// Function to find lexicographic smallest
// string consisting of the string str
// as prefix
string lexicographicallyString(
string input[], int n, string str)
{
// Sort the given array string arr[]
sort(input, input + n);
for (int i = 0; i < n; i++) {
string temp = input[i];
// If the i-th string contains
// given string as a prefix,
// then print the result
if (is_prefix(temp, str)) {
return temp;
}
}
// If no string exists then
// return "-1"
return "-1";
}
// Driver Code
int main()
{
string arr[] = { "apple", "appe", "apl",
"aapl", "appax" };
string S = "app";
int N = 5;
cout << lexicographicallyString(
arr, N, S);
return 0;
}
Java
// Java program for the above approach
import java.util.Arrays;
class GFG {
// Function to find the whether the
// string temp starts with str or not
static boolean is_prefix(String temp, String str)
{
// Base Case
if (temp.length() < str.length())
return false;
else {
// Check for the corresponding
// characters in temp & str
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != temp.charAt(i))
return false;
}
return true;
}
}
// Function to find lexicographic smallest
// string consisting of the string str
// as prefix
static String lexicographicallyString(String[] input,
int n, String str)
{
// Sort the given array string arr[]
Arrays.sort(input);
for (int i = 0; i < n; i++) {
String temp = input[i];
// If the i-th string contains
// given string as a prefix,
// then print the result
if (is_prefix(temp, str)) {
return temp;
}
}
// If no string exists then
// return "-1"
return "-1";
}
// Driver Code
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));
}
}
// This code is contributed by AnkThon
Python3
# Python 3 program for the above approach
# Function to find the whether the
# string temp starts with str or not
def is_prefix(temp, str):
# Base Case
if (len(temp) < len(str)):
return 0
else:
# Check for the corresponding
# characters in temp & str
for i in range(len(str)):
if (str[i] != temp[i]):
return 0
return 1
# Function to find lexicographic smallest
# string consisting of the string str
# as prefix
def lexicographicallyString(input, n, str):
# Sort the given array string arr[]
input.sort()
for i in range(n):
temp = input[i]
# If the i-th string contains
# given string as a prefix,
# then print the result
if (is_prefix(temp, str)):
return temp
# If no string exists then
# return "-1"
return "-1"
# Driver Code
if __name__ == '__main__':
arr = ["apple", "appe", "apl", "aapl", "appax"]
S = "app"
N = 5
print(lexicographicallyString(arr, N, S))
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the whether the
// string temp starts with str or not
static bool is_prefix(string temp, string str)
{
// Base Case
if (temp.Length < str.Length)
return false;
else {
// Check for the corresponding
// characters in temp & str
for (int i = 0; i < str.Length; i++) {
if (str[i] != temp[i])
return false;
}
return true;
}
}
// Function to find lexicographic smallest
// string consisting of the string str
// as prefix
static string lexicographicallyString(string[] input,
int n, string str)
{
// Sort the given array string arr[]
Array.Sort(input);
for (int i = 0; i < n; i++) {
string temp = input[i];
// If the i-th string contains
// given string as a prefix,
// then print the result
if (is_prefix(temp, str)) {
return temp;
}
}
// If no string exists then
// return "-1"
return "-1";
}
// Driver Code
public static void Main()
{
string[] arr
= { "apple", "appe", "apl", "aapl", "appax" };
string S = "app";
int N = 5;
Console.WriteLine(
lexicographicallyString(arr, N, S));
}
}
// This code is contributed by ukasp.
Javascript
appax
时间复杂度: O(M*K*N*log N),其中 K 是数组arr[]中字符串的最大长度。
辅助空间: O(N)
另一种方法:上述方法也可以通过使用 Trie 数据结构进行优化,方法是在 Trie 中插入所有给定的字符串,然后检查存在于 Trie 中的第一个字符串是否具有前缀S 。
时间复杂度: O(M*N)
辅助空间: O(N)