JSFiddle - React, Tailwind, and code Playground

by Ben Gillbanks

JavaScript

function findPotentialWordsUsingAllLetters(letters) {
  const potentialWords = [];

  function isPotentialWord(word) {
    // Add your own heuristics to determine if a word is potential
    // For simplicity, we'll consider a word with 3 or more letters as potential.
    return word.length >= 3;
  }

  function permute(letters, word = '') {
    if (word.length > 0) {
      if (isPotentialWord(word)) {
        potentialWords.push(word);
      }
    }

    if (letters.length === 0) {
      return;
    }

    for (let i = 0; i < letters.length; i++) {
      const remainingLetters = letters.slice(0, i) + letters.slice(i + 1);
      permute(remainingLetters, word + letters[i]);
    }
  }

  permute(letters);

  return potentialWords;
}

const randomLetters = "aelpp";
const allPossibleWords = findPotentialWordsUsingAllLetters(randomLetters);

console.log(allPossibleWords);