Answers for "find all possible combinations of letters javascript"

0

find all possible combinations of letters javascript

// read the question from stackexchange for more details
// https://codereview.stackexchange.com/questions/57161/generate-all-possible-combinations-of-letters-in-a-word

function swap(chars, i, j) {
    var tmp = chars[i];
    chars[i] = chars[j];
    chars[j] = tmp;
}

function getAnagrams(input) {
    var counter = [],
        anagrams = [],
        chars = input.split(''),
        length = chars.length,
        i;

    for (i = 0; i < length; i++) {
        counter[i] = 0;
    }

    anagrams.push(input);
    i = 0;
    while (i < length) {
        if (counter[i] < i) {
            swap(chars, i % 2 === 1 ? counter[i] : 0, i);
            counter[i]++;
            i = 0;
            anagrams.push(chars.join(''));
        } else {
            counter[i] = 0;
            i++;
        }
    }

    return anagrams;
}
Posted by: Guest on June-14-2021

Code answers related to "find all possible combinations of letters javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language