给定一个单词数组,找到所有最短的唯一前缀以表示给定数组中的每个单词。假设没有一个单词是另一个单词的前缀。按排序顺序输出最短的唯一前缀。
Input : {"zebra", "dog", "duck", "dove"}
Output : z, dog, dov, du
Explanation: dog => dog
dove = dov
duck = du
z => zebra
Input: {"geeksgeeks", "geeksquiz", "geeksforgeeks"}
Output: geeksf, geeksg, geeksq
我们在下面的文章中讨论了基于Trie的方法。
查找给定列表中每个单词的最短唯一前缀|设置1(使用特里)
在这篇文章中,讨论了一种基于排序的方法。将字符串与数组中的其他2个最相似的字符串进行比较时,我们可以找到其最短的唯一前缀。例如,如果我们对数组{“ zebra”,“ dog”,“ duck”,“ dove”}进行排序,则会得到{“ dog”,“ dove”,“ duck”,“ zebra”}。字符串“ dove”的最短唯一前缀可以找到:
比较“鸽子”与“狗” –>鸽子的唯一前缀是“ dov”
比较“鸽子”与“鸭子” –>鸽子的唯一前缀是“ do”
现在,“鸽子”的最短唯一前缀是在“ dov”和“ do”之间具有更大长度的前缀。因此,它是“ dov”。
通过将第一个和最后一个字符串分别与右边和左边分别只有一个最相似的邻居进行比较,可以找到最短的唯一前缀。
我们可以对字符串数组进行排序,并继续对数组的每个字符串执行此操作。
C++
// C++ program to print shortest unique prefixes
// for every word.
#include
using namespace std;
vector uniquePrefix(vector &a)
{
int size = a.size();
/* create an array to store the results */
vector res(size);
/* sort the array of strings */
sort(a.begin(), a.end());
/* compare the first string with its only right
neighbor */
int j = 0;
while (j < min(a[0].length() - 1, a[1].length() - 1))
{
if (a[0][j] == a[1][j])
j++;
else
break;
}
int ind = 0;
res[ind++] = a[0].substr(0, j + 1);
/* Store the unique prefix of a[1] from its left neighbor */
string temp_prefix = a[1].substr(0, j + 1);
for (int i = 1; i < size - 1; i++)
{
/* compute common prefix of a[i] unique from
its right neighbor */
j = 0;
while (j < min(a[i].length() - 1, a[i + 1].length() - 1))
{
if (a[i][j] == a[i + 1][j])
j++;
else
break;
}
string new_prefix = a[i].substr(0, j + 1);
/* compare the new prefix with previous prefix */
if (temp_prefix.length() > new_prefix.length())
res[ind++] = temp_prefix;
else
res[ind++] = new_prefix;
/* store the prefix of a[i+1] unique from its
left neighbour */
temp_prefix = a[i + 1].substr(0, j + 1);
}
/* compute the unique prefix for the last string
in sorted array */
j = 0;
string sec_last = a[size - 2];
string last = a[size - 1];
while (j < min(sec_last.length() - 1, last.length() - 1))
{
if (sec_last[j] == last[j])
j++;
else
break;
}
res[ind] = last.substr(0, j + 1);
return res;
}
// Driver Code
int main()
{
vector input = {"zebra", "dog", "duck", "dove"};
vector output = uniquePrefix(input);
cout << "The shortest unique prefixes in sorted order are : \n";
for (auto i : output)
cout << i << ' ';
return 0;
}
// This code is contributed by
// sanjeev2552
Java
// Java program to print shortest unique prefixes
// for every word.
import java.io.*;
import java.util.*;
class GFG
{
public String[] uniquePrefix(String[] a)
{
int size = a.length;
/* create an array to store the results */
String[] res = new String[size];
/* sort the array of strings */
Arrays.sort(a);
/* compare the first string with its only right
neighbor */
int j = 0;
while (j < Math.min(a[0].length()-1, a[1].length()-1))
{
if (a[0].charAt(j)==a[1].charAt(j))
j++;
else
break;
}
int ind = 0;
res[ind++] = a[0].substring(0, j+1);
/* Store the unique prefix of a[1] from its left neighbor */
String temp_prefix = a[1].substring(0, j+1);
for (int i = 1; i < size-1; i++)
{
/* compute common prefix of a[i] unique from
its right neighbor */
j = 0;
while (j < Math.min( a[i].length()-1, a[i+1].length()-1 ))
{
if (a[i].charAt(j) == a[i+1].charAt(j))
j++;
else
break;
}
String new_prefix = a[i].substring(0, j+1);
/* compare the new prefix with previous prefix */
if (temp_prefix.length() > new_prefix.length() )
res[ind++] = temp_prefix;
else
res[ind++] = new_prefix;
/* store the prefix of a[i+1] unique from its
left neighbour */
temp_prefix = a[i+1].substring(0, j+1);
}
/* compute the unique prefix for the last string
in sorted array */
j = 0;
String sec_last = a[size-2] ;
String last = a[size-1];
while (j < Math.min( sec_last.length()-1, last.length()-1))
{
if (sec_last.charAt(j) == last.charAt(j))
j++;
else
break;
}
res[ind] = last.substring(0, j+1);
return res;
}
/* Driver Function to test other function */
public static void main(String[] args)
{
GFG gfg = new GFG();
String[] input = {"zebra", "dog", "duck", "dove"};
String[] output = gfg.uniquePrefix(input);
System.out.println( "The shortest unique prefixes" +
" in sorted order are :");
for (int i=0; i < output.length; i++)
System.out.print( output[i] + " ");
}
}
C#
// C# program to print shortest unique prefixes
// for every word.
using System;
class GFG
{
public String[] uniquePrefix(String[] a)
{
int size = a.Length;
/* create an array to store the results */
String[] res = new String[size];
/* sort the array of strings */
Array.Sort(a);
/* compare the first string with its only right
neighbor */
int j = 0;
while (j < Math.Min(a[0].Length - 1, a[1].Length - 1))
{
if (a[0][j] == a[1][j])
j++;
else
break;
}
int ind = 0;
res[ind++] = a[0].Substring(0, j + 1);
/* Store the unique prefix of a[1] from its left neighbor */
String temp_prefix = a[1].Substring(0, j + 1);
for (int i = 1; i < size - 1; i++)
{
/* compute common prefix of a[i] unique from
its right neighbor */
j = 0;
while (j < Math.Min( a[i].Length - 1, a[i + 1].Length - 1 ))
{
if (a[i][j] == a[i + 1][j])
j++;
else
break;
}
String new_prefix = a[i].Substring(0, j+1);
/* compare the new prefix with previous prefix */
if (temp_prefix.Length > new_prefix.Length )
res[ind++] = temp_prefix;
else
res[ind++] = new_prefix;
/* store the prefix of a[i+1] unique from its
left neighbour */
temp_prefix = a[i+1].Substring(0, j+1);
}
/* compute the unique prefix for the last string
in sorted array */
j = 0;
String sec_last = a[size-2] ;
String last = a[size-1];
while (j < Math.Min( sec_last.Length-1, last.Length-1))
{
if (sec_last[j] == last[j])
j++;
else
break;
}
res[ind] = last.Substring(0, j+1);
return res;
}
/* Driver code */
public static void Main(String[] args)
{
GFG gfg = new GFG();
String[] input = {"zebra", "dog", "duck", "dove"};
String[] output = gfg.uniquePrefix(input);
Console.WriteLine( "The shortest unique prefixes" +
" in sorted order are :");
for (int i = 0; i < output.Length; i++)
Console.Write( output[i] + " ");
}
}
// This code is contributed by Princi Singh
Python3
#Python program to print shortest unique prefix for every word in a list
a=['dogs','dove','duck','zebra']
r=[]
j=0
while(jlen(y)):
r.append(x)
else:
r.append(y)
x=a[i+1][0:j+1]
j=0
l=a[len(a)-2]
k=a[len(a)-1]
while(j
输出:
The shortest unique prefixes in sorted order are :
dog dov du z
另一种Python方法:
如果要在输入数组中将前缀作为字符串的顺序输出,则可以将字符串及其对应的索引存储在哈希图中。在将前缀添加到结果数组时,我们可以从哈希图中获取相应字符串的索引,并将前缀添加到该索引。
Python3
#Python program to print shortest unique prefix for every word in a list
a=['dogs','dove','duck','zebra']
r=[]
j=0
while(jlen(y)):
r.append(x)
else:
r.append(y)
x=a[i+1][0:j+1]
j=0
l=a[len(a)-2]
k=a[len(a)-1]
while(j
输出:
The shortest unique prefixes are :
dog dov du z