Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Algorithm:</p> <ol> <li>Break the target string into letters and group them, get count of each letter</li> <li>Form all permutations of strings in the array, starting with 1 string and up to the entire array. If your string array is short (i.e. &lt;32), you can have an auto-increment integer with bitmask to generate all the permutations. If your string array is long (>32), then use an array to store each "bit" slot with string length, and simulate incrementing integer.</li> <li>Skip all permutations not of the same length as target string (this should eliminate 90% of all permutations). Get the total letter count by summing the number of "1" bits x length of string (or summing slots); it must = target string length.</li> <li>For each permutation, breakdown down the letters and group them, get count of each letter</li> <li>Run through the target string group letter by letter, compare count to the group of the string. The letters and counts must be exactly the same to succeed. If so, return that permutation as the answer.</li> <li>Otherwise continue with another permutation</li> <li>After going through all permutations, return fail.</li> </ol> <p>A JavaScript implementation:</p> <pre><code>// Assume: target=target string, words_array=array of strings function groupByLetters(map, text) { for (var x=0; x &lt; text.length; x++) { var ch = text.charAt(x); var n = map[ch] || 0; map[ch] = n + 1; } } // Split the target string into letters var target_map = {}; groupByLetters(target_map, target); // Create permutation slots var slots = []; for (var x=0; x &lt; words_array.length; x++) { // Now in order to optimize speed, store the length of each string in the slot // Negative = not selected, positive = selected slots.push(-words_array[x].length); } // Loop through all permutations while(true) { var carry = true; var plength = 0; for (var x=0; x &lt; slots.length; x++) { var slen = slots[x]; if (carry) { if (slen &lt; 0) { // Bit 0 carry = false; slots[x] = -slen; // 0-&gt;1, no carry } else { slots[x] = -slen; // 1-&gt;0, continue to carry } } if (slots[x] &gt; 0) plength += slots[x]; } if (carry) { // We have exhausted the permutations return null; } // Now plength = total number of letters in selected permutation if (plength !== target.length) continue; // Not the same number of letters, skip // Build map of all letters in selected permutation var pmap = {}; var permutation = []; for (var x=0; x &lt; slots.length; x++) { if (slots[x] &gt; 0) { groupByLetters(pmap, words_array[x]); permutation.push(words_array[x]); } } // Check if the map is the same as the target map var match = true; for (var letter in target_map) { if (!target_map.hasOwnProperty(letter)) continue; if (target_map[letter] !== pmap[letter]) { match = false; break; } } if (match) return permutation; // Success! } </code></pre> <p>Caveat: I have <strong>not</strong> tried running this. Let me know if I made a typo here or there.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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