The Wayback Machine - https://web.archive.org/web/20211221123237/https://www.geeksforgeeks.org/print-all-valid-words-from-given-dictionary-that-are-possible-using-characters-of-given-array/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Print all valid words from given dictionary that are possible using Characters of given Array

  • Last Updated : 21 Dec, 2021

Given a dictionary of strings dict[] and a character array arr[]. Print all valid words of the dictionary that are possible using characters from the given character array.
Examples:

Input: dict – [“go”, “bat”, “me”, “eat”, “goal”, boy”, “run”] 
arr[] = [‘e’, ‘o’, ‘b’, ‘a’, ‘m’, ‘g’, ‘l’]
Output: “go”, “me”, “goal”.
Explanation: Only all the characters of these three strings are present in the dictionary.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: Dict=  [“goa”, “bait”, “mess”, “ate”, “goal”, “girl”, “rain”]
arr[]= [‘s’, ‘o’, ‘t’, ‘a’, ‘e’, ‘g’, ‘l’, ‘i’, ‘r’]
Output: “goa”, “ate”, “goal”, “girl”



 

Approach: The problem can be solved by checking the characters of each string of the dictionary. If all the characters of a string is present then that string can b formed. Follow the steps mentioned below to solve the problem.

  • Concatenate all the characters and make a string.
  • Traverse each word in a string and find whether all the characters of each word are present in concatenated string or not.

Follow the illustration shown below for better understanding.

Illustration: Consider the example below.

dict[] = [“go”, “bat”, “eat”, “meat”, “goal”, “boy”, “run”],  
arr[] = [‘e’, ‘o’, ‘b’, ‘a’, ‘m’, ‘g’, ‘l’]

  • concatenated string= e o b a m g l
  • now if we traverse “go”
  • indexOf(“g”) in “eobamgl” is 5
  • indexOf(“o”) in “eobamgl” is 1
  • This means all the indexes are present. Hence we will print “go”
  • If any of the indexes are not present, that will not be included.
  • Similarly “me” and “goal” also satisfies the condition.

So the output is “go”, “goal” and “me”.

Below is the implementation of the above approach.

Javascript




<script>
    // JavaScript code to implement the above approach
  
    // Function to print the words
    function printWord(str, s) {
        for (var i = 0; i < str.length; i++) {
            if (s.indexOf(str[i]) < 0) {
                return;
            }
        }
        document.write(str);
        document.write("<br>");
    }
      
    // Function to find the words
    function findWord(str1, str2) {
        var s = "";
        for (var i in str2) {
            s += str2[i];
        }
        for (var i = 0; i < str1.length; i++) {
            printWord(str1[i], s);
        }
    }
      
    var str1 = ["go", "bat", "me", "eat",
                "goal", "boy", "run"];
    var str2 = ["e", "o", "b", "a", "m", "g", "l"];
    findWord(str1, str2);
</script>

Output:

go
me
goal

Time Complexity: O(N * K) where N is the length of the dict[] and k is the length of arr[].
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!