📌  相关文章
📜  使用给定数组的字符打印给定字典中所有可能的有效单词

📅  最后修改于: 2022-05-13 01:57:07.248000             🧑  作者: Mango

使用给定数组的字符打印给定字典中所有可能的有效单词

给定一个字符串字典 dict[]和一个字符数组arr[] 。打印字典中所有可能使用给定字符数组中的字符的有效单词。
例子:

方法:可以通过检查字典中每个字符串的字符来解决问题。如果字符串的所有字符都存在,则可以形成该字符串。请按照以下步骤解决问题。

  • 连接所有字符并创建一个字符串。
  • 遍历字符串中的每个单词,并查找每个单词的所有字符是否都存在于连接的字符串中。

请按照下图进行更好的理解。

下面是上述方法的实现。

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
// Function to print the words
void printWord(string str, string s) {
    for (int i = 0; i < str.size(); i++) {
        if (s.find(str[i]) == string::npos) {
            return;
        }
    }
    cout << str << endl;
}
 
// Function to find the words
void findWord(vector str1, vector str2) {
    string s = "";
    for (int i = 0; i < str2.size(); i++) {
        s += str2[i];
    }
    for (int i = 0; i < str1.size(); i++) {
        printWord(str1[i], s);
    }
}
 
int main() {
    vector str1 = {"go", "bat", "me", "eat",
                "goal", "boy", "run"};
    vector str2 = {'e', 'o', 'b', 'a', 'm', 'g', 'l'};
    findWord(str1, str2);
    return 0;
}
 
// This code is contributed by Samim Hossain Mondal.


Java
// Java code to implement the above approach
class GFG{
 
  // Function to print the words
  public static void printWord(String str, String s) {
    for (int i = 0; i < str.length(); i++) {
      if (s.indexOf(str.charAt(i)) < 0) {
        return;
      }
    }
    System.out.println(str);
  }
 
  // Function to find the words
  public static void findWord(String[] str1, char[] str2) {
    String s = "";
    for (int i = 0; i < str2.length; i++) {
      s += str2[i];
    }
    for (int i = 0; i < str1.length; i++) {
      printWord(str1[i], s);
    }
  }
 
  public static void main(String args[]) {
    String[] str1 = {"go", "bat", "me", "eat", "goal", "boy", "run"};
    char[] str2 = {'e', 'o', 'b', 'a', 'm', 'g', 'l'};
    findWord(str1, str2);
  }
}
 
// This code is contributed by Saurabh Jaiswal


Python3
# python code to implement the above approach
 
# Function to print the words
def printWord(str, s):
    for i in range(0, len(str)):
        if (not (str[i] in s)):
            return
 
    print(str)
 
# Function to find the words
def findWord(str1, str2):
    s = ""
    for i in str2:
        s += i
 
    for i in range(0, len(str1)):
        printWord(str1[i], s)
 
# Driver Code
if __name__ == "__main__":
    str1 = ["go", "bat", "me", "eat", "goal", "boy", "run"]
 
    str2 = ["e", "o", "b", "a", "m", "g", "l"]
 
    findWord(str1, str2)
 
    # This code is contributed by rakeshsahni


C#
// C# code to implement the above approach
using System;
 
class GFG {
 
  // Function to print the words
  public static void printWord(string str, string s)
  {
    for (int i = 0; i < str.Length; i++) {
      if (s.IndexOf((str[i])) < 0) {
        return;
      }
    }
    Console.WriteLine(str);
  }
 
  // Function to find the words
  public static void findWord(string[] str1, char[] str2)
  {
    string s = "";
    for (int i = 0; i < str2.Length; i++) {
      s += str2[i];
    }
    for (int i = 0; i < str1.Length; i++) {
      printWord(str1[i], s);
    }
  }
 
  public static void Main(string[] args)
  {
    string[] str1 = { "go",   "bat", "me", "eat",
                     "goal", "boy", "run" };
    char[] str2 = { 'e', 'o', 'b', 'a', 'm', 'g', 'l' };
    findWord(str1, str2);
  }
}
 
// This code is contributed by ukasp.


Javascript


输出:

go
me
goal

时间复杂度: O(N * K),其中 N 是 dict[] 的长度,k 是 arr[] 的长度。
辅助空间: O(1)