给定N个字符串[]阵列ARR以及表示字符串的新字母顺序的字符串顺序。任务是根据给定的顺序查找字典上最大的字符串。
例子:
Input: a[] = {“abc”, “abd”, “abz”}, order = “abczdefghijklmnopqrstuvwxy”
Output: abd
Explanation:
Compare two words “abc”, “abd”, the first non-matching character is c, d in the order, c comes before d so abd is largest among them.
Similarly, compare abd and abz.
Input: arr[] = {“abc”, “abdz”, “abd”}, order = “abcdefghijklmnopqrstuvwxyz”
Output: abdz
Explanation:
Among all the given strings abdz is the largest.
天真的方法:这个想法是检查每个字符串在字典上在给定字符串是否最大。如果是,则打印该字符串,否则检查下一个字符串。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:想法是根据给定的字符串顺序实现比较器函数,以找到词典上最大的字符串。步骤如下:
- 创建一个映射,以给定的字符串顺序存储字符的索引。
- 将数组的第一个字符串视为字典上最大的字符串,如ans 。
- 现在遍历在范围[1,N]给定的字符串,并比较每个字符串用的字符串使用存储在地图索引ANS。
- 继续在上述步骤中更新最大的词典字符串并打印该字符串。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
int compare(string word1, string word2,
int order[]);
// Find the lexicographically
// largest string
string largestString(string a[], int n,
string order)
{
// Create a map of characters
int map[26];
// Value of each character is
// string is given priority
// according to their occurence
// in the string
for(int i = 0; i < order.length(); i++)
map[order[i] - 'a'] = i;
// Take first String as maximum
string ans = a[0];
for(int i = 1; i < n; i++)
{
// Compare two strings each time
if (compare(ans, a[i], map) < 0)
// Update answer
ans = a[i];
}
return ans;
}
// Implement compare function
// to get the dictionary order
int compare(string word1, string word2,
int order[])
{
int i = 0, j = 0, charcompareval = 0;
while (i < word1.length() &&
j < word2.length())
{
// Compare each char
// according to the order
charcompareval = order[word1[i] - 'a'] -
order[word2[i] - 'a'];
// Find the first non matching
// character in the string
if (charcompareval != 0)
return charcompareval;
i++;
j++;
}
// If one word is prefix of
// other return shortest word
if (charcompareval == 0)
return (word1.length() -
word2.length());
else
return charcompareval;
}
// Driver Code
int main()
{
int n = 3;
// Given array of strings arr
string arr[] = { "abc", "abd", "abz" };
// Given order of string
string order = "abczdefghijklmnopqrstuvwxy";
// Function call
string ans = largestString(arr, n, order);
cout << ans;
return 0;
}
// This code is contributed by rutvik_56
Java
// Java program for the above approach
import java.util.*;
public class Main {
// Find the lexicographically
// largest string
public static String
largestString(String[] a, int n,
String order)
{
// Create a map of characters
int map[] = new int[26];
// Value of each character is
// string is given priority
// according to their occurence
// in the string
for (int i = 0; i < order.length(); i++)
map[order.charAt(i) - 'a'] = i;
// Take first String as maximum
String ans = a[0];
for (int i = 1; i < n; i++) {
// Compare two strings each time
if (compare(ans, a[i], map) < 0)
// Update answer
ans = a[i];
}
return ans;
}
// Implement compare function
// to get the dictionary order
public static int
compare(String word1, String word2,
int[] order)
{
int i = 0, j = 0, charcompareval = 0;
while (i < word1.length()
&& j < word2.length()) {
// Compare each char
// according to the order
charcompareval
= order[word1.charAt(i) - 'a']
- order[word2.charAt(i) - 'a'];
// Find the first non matching
// character in the string
if (charcompareval != 0)
return charcompareval;
i++;
j++;
}
// If one word is prefix of
// other return shortest word
if (charcompareval == 0)
return (word1.length()
- word2.length());
else
return charcompareval;
}
// Driver Code
public static void main(String args[])
{
int n = 3;
// Given array of strings arr
String arr[] = { "abc", "abd", "abz" };
// Given order of string
String order
= "abczdefghijklmnopqrstuvwxy";
// Function call
String ans
= largestString(arr, n, order);
System.out.println(ans);
}
}
Python3
# Python3 program for the above approach
# Find the lexicographically
# largest string
def largestString(a, n, order):
# Create a map of characters
map = [0] * 26
# Value of each character is
# string is given priority
# according to their occurence
# in the string
for i in range(len(order)):
map[ord(order[i]) - ord('a')] = i
# Take first String as maximum
ans = a[0]
for i in range(1, n):
# Compare two strings each time
if (compare(ans, a[i], map) < 0):
# Update answer
ans = a[i]
return ans
# Implement compare function
# to get the dictionary order
def compare(word1, word2, order):
i = 0
j = 0
charcompareval = 0;
while (i < len(word1) and
j < len(word2)):
# Compare each char
# according to the order
charcompareval = (order[ord(word1[i]) - ord('a')] -
order[ord(word2[i]) - ord('a')])
# Find the first non matching
# character in the string
if (charcompareval != 0):
return charcompareval
i += 1
j += 1
# If one word is prefix of
# other return shortest word
if (charcompareval == 0):
return (len(word1) - len(word2))
else:
return charcompareval
# Driver Code
if __name__ == "__main__":
n = 3
# Given array of strings arr
arr = [ "abc", "abd", "abz" ]
# Given order of string
order = "abczdefghijklmnopqrstuvwxy"
# Function call
ans = largestString(arr, n, order)
print(ans)
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
class GFG{
// Find the lexicographically
// largest string
public static String largestString(String[] a, int n,
String order)
{
// Create a map of characters
int []map = new int[26];
// Value of each character is
// string is given priority
// according to their occurence
// in the string
for (int i = 0; i < order.Length; i++)
map[order[i] - 'a'] = i;
// Take first String as maximum
String ans = a[0];
for (int i = 1; i < n; i++)
{
// Compare two strings each time
if (compare(ans, a[i], map) < 0)
// Update answer
ans = a[i];
}
return ans;
}
// Implement compare function
// to get the dictionary order
public static int compare(String word1,
String word2,
int[] order)
{
int i = 0, j = 0, charcompareval = 0;
while (i < word1.Length &&
j < word2.Length)
{
// Compare each char
// according to the order
charcompareval = order[word1[i] - 'a'] -
order[word2[i] - 'a'];
// Find the first non matching
// character in the string
if (charcompareval != 0)
return charcompareval;
i++;
j++;
}
// If one word is prefix of
// other return shortest word
if (charcompareval == 0)
return (word1.Length -
word2.Length);
else
return charcompareval;
}
// Driver Code
public static void Main(String []args)
{
int n = 3;
// Given array of strings arr
String []arr = { "abc", "abd", "abz" };
// Given order of string
String order = "abczdefghijklmnopqrstuvwxy";
// Function call
String ans = largestString(arr, n, order);
Console.WriteLine(ans);
}
}
// This code is contributed by Amit Katiyar
abd
时间复杂度: O(N * max_word_length)
辅助空间: O(1)