DEV Community

Discussion on: Daily Challenge #182 - Arrh, grabscrab!

Collapse
 
bhaumin profile image
Bhaumin Shah

JavaScript solution:

function grabscrab(word, dict) {
  const wordSorted = word.split("").sort().join("");
  const ans = [];

  for (let item of dict) {
    itemSorted = item.split("").sort().join("");
    if (itemSorted === wordSorted) {
      ans.push(item);
    }
  }

  return ans;
}
  1. Sort the input word just once outside the loop
  2. Just compare the full sorted words instead of each char to avoid false positives in comparing words like aabbbcc to aaabccc