给定一个字符串数组str[]和一个字符串key ,任务是检查键的拼写是否正确。如果发现是真的,则打印“YES” 。否则,打印建议的正确拼写。
例子:
Input:str[] = { “gee”, “geeks”, “ape”, “apple”, “geeksforgeeks” }, key = “geek”
Output: geeks geeksforgeeks
Explanation:
The string “geek” not present in the array of strings.
Therefore, the suggested words are { “geeks”, “geeksforgeeks” }.
Input: str[] = { “gee”, “geeks”, “ape”, “apple”, “arp” }, key = “geeks”
Output: YES.
方法:使用Trie可以解决这个问题。我们的想法是遍历字符串,STR [阵列],然后插入字符串入Trie树使得Trie树中的每个节点包含字符串和一个布尔值的字符,以检查是否该字符是字符串的最后一个字符或不是。请按照以下步骤解决问题:
- 初始化一个Trie树,比方说根,使得Trie树中的每个节点都包括一个字符串的字符和一个布尔值的检查,如果字符是字符串或不是最后一个字符。
- 遍历字符串数组arr[] ,并将所有字符串插入到 Trie 中。
- 最后,遍历字符串key 。对于每个第i个字符,检查该字符是否存在于Trie 中。如果发现为真,则移动到 Trie 的下一个节点。
- 否则,打印所有可能的字符串,其前缀是字符串key 。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Structure of a Trie node
struct TrieNode {
// Store address of a character
TrieNode* Trie[256];
// Check if the character is
// last character of a string or not
bool isEnd;
// Constructor function
TrieNode()
{
for (int i = 0; i < 256; i++) {
Trie[i] = NULL;
}
isEnd = false;
}
};
// Function to insert a string into Trie
void InsertTrie(TrieNode* root, string s)
{
TrieNode* temp = root;
// Traverse the string, s
for (int i = 0; i < s.length(); i++) {
if (temp->Trie[s[i]] == NULL) {
// Initialize a node
temp->Trie[s[i]] = new TrieNode();
}
// Update temp
temp = temp->Trie[s[i]];
}
// Mark the last character of
// the string to true
temp->isEnd = true;
}
// Function to print suggestions of the string
void printSuggestions(TrieNode* root, string res)
{
// If current character is
// the last character of a string
if (root->isEnd == true) {
cout << res << " ";
}
// Iterate over all possible
// characters of the string
for (int i = 0; i < 256; i++) {
// If current character
// present in the Trie
if (root->Trie[i] != NULL) {
// Insert current character
// into Trie
res.push_back(i);
printSuggestions(root->Trie[i], res);
res.pop_back();
}
}
}
// Function to check if the string
// is present in Trie or not
bool checkPresent(TrieNode* root, string key)
{
// Traverse the string
for (int i = 0; i < key.length(); i++) {
// If current character not
// present in the Trie
if (root->Trie[key[i]] == NULL) {
printSuggestions(root, key.substr(0, i));
return false;
}
// Update root
root = root->Trie[key[i]];
}
if (root->isEnd == true) {
return true;
}
printSuggestions(root, key);
return false;
}
// Driver Code
int main()
{
// Given array of strings
vector str = { "gee", "geeks", "ape",
"apple", "geeksforgeeks" };
string key = "geek";
// Initialize a Trie
TrieNode* root = new TrieNode();
// Insert strings to trie
for (int i = 0; i < str.size(); i++) {
InsertTrie(root, str[i]);
}
if (checkPresent(root, key)) {
cout << "YES";
}
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
// Structure of a Trie node
class TrieNode
{
// Store address of a character
TrieNode Trie[];
// Check if the character is
// last character of a string or not
boolean isEnd;
// Constructor function
public TrieNode()
{
Trie = new TrieNode[256];
for(int i = 0; i < 256; i++)
{
Trie[i] = null;
}
isEnd = false;
}
}
class GFG{
// Function to insert a string into Trie
static void InsertTrie(TrieNode root, String s)
{
TrieNode temp = root;
// Traverse the string, s
for(int i = 0; i < s.length(); i++)
{
if (temp.Trie[s.charAt(i)] == null)
{
// Initialize a node
temp.Trie[s.charAt(i)] = new TrieNode();
}
// Update temp
temp = temp.Trie[s.charAt(i)];
}
// Mark the last character of
// the string to true
temp.isEnd = true;
}
// Function to print suggestions of the string
static void printSuggestions(TrieNode root, String res)
{
// If current character is
// the last character of a string
if (root.isEnd == true)
{
System.out.print(res + " ");
}
// Iterate over all possible
// characters of the string
for(int i = 0; i < 256; i++)
{
// If current character
// present in the Trie
if (root.Trie[i] != null)
{
// Insert current character
// into Trie
res += (char)i;
printSuggestions(root.Trie[i], res);
res = res.substring(0, res.length() - 2);
}
}
}
// Function to check if the string
// is present in Trie or not
static boolean checkPresent(TrieNode root, String key)
{
// Traverse the string
for(int i = 0; i < key.length(); i++)
{
// If current character not
// present in the Trie
if (root.Trie[key.charAt(i)] == null)
{
printSuggestions(root, key.substring(0, i));
return false;
}
// Update root
root = root.Trie[key.charAt(i)];
}
if (root.isEnd == true)
{
return true;
}
printSuggestions(root, key);
return false;
}
// Driver Code
public static void main(String[] args)
{
// Given array of strings
String str[] = { "gee", "geeks", "ape", "apple",
"geeksforgeeks" };
String key = "geek";
// Initialize a Trie
TrieNode root = new TrieNode();
// Insert strings to trie
for(int i = 0; i < str.length; i++)
{
InsertTrie(root, str[i]);
}
if (checkPresent(root, key))
{
System.out.println("YES");
}
}
}
// This code is contributed by Dharanendra L V.
输出:
geeks geeksforgeeks
时间复杂度: O(N * M),其中 M 是字符串的最大长度
辅助空间: O(N * 256)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。