Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a simple approach: For your source word, create an array of size 26 and use it to count the how many times each letter appears. Do the same for each word in your dictionary. Then compare the two. If every letter occurs less than or equal to as many times in the dictionary word as the source word, then it can be used to make that word. If not, then it cannot.</p> <p>C-Sharpish Pseudocode: (probably doesn't compile as written)</p> <pre><code>/** Converts characters to a 0 to 25 code representing alphabet position. This is specific to the English language and would need to be modified if used for other languages. */ int charToLetter(char c) { return Char.ToUpper(c)-'A'; } /** Given a source word and an array of other words to check, returns all words from the array which can be made from the letters of the source word. */ ArrayList&lt;string&gt; checkSubWords(string source, string[] dictionary) { ArrayList&lt;string&gt; output = new ArrayList&lt;string&gt;(); // Stores how many of each letter are in the source word. int[] sourcecount = new int[26]; // Should initialize to 0, automatically foreach (char c in source) { sourcecount[c]++; } foreach (string s in dictionary) { // Stores how many of each letter are in the dictionary word. int[] dictcount = new int[26]; // Should initialize to 0, automatically foreach (char c in s) { dictcount[c]++; } // Then we check that there exist no letters which appear more in the // dictionary word than the source word. boolean isSubword = true; for (int i=0;i&lt;26;i++) { if (dictcount[i] &gt; sourcecount[i]) { isSubword = false; } } // If they're all less than or equal to, then we add it to the output. if (isSubWord) { output.add(s); } } return output; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload