Skip to content

Instantly share code, notes, and snippets.

@dawnerd
Created January 7, 2011 23:24
Show Gist options
  • Save dawnerd/770299 to your computer and use it in GitHub Desktop.
Save dawnerd/770299 to your computer and use it in GitHub Desktop.
Meow
var letters = "apples".split("").sort().join(""), words = ["pale", "ales", "ape", "grape", "pape", "pppp"];
document.writeln("Matching words to: " + letters);
for(var i = 0, c = words.length; i < c; i++) {
var word = words[i].split("").sort().join(""), status = true, usedChars = [];
for(var ii = 0, cc = word.length-1; ii < cc; ii++) var lidx = letters.indexOf(word[ii], ii), idx = usedChars.indexOf(lidx), status = ((idx == -1 && lidx != -1) ? new Boolean(usedChars.push(lidx)) : false);
document.writeln(words[i] + " " + status);
}
@listenrightmeow
Copy link

var letters = "apples".split("").sort().join(""),
words = ["pale", "ales", "ape", "grape", "pape", "pp"];

document.writeln("Matching words to: " + letters);

for(var i = 0, c = words.length; i < c; i++) {

var word = words[i].split("").sort().join(""), 
    status = true, 
    letterObj = {};

for (var ui = 0; ui < letters.length; ui++) {
    //document.writeln(letters[ui]);
    if (!letterObj[letters[ui]])
        letterObj[letters[ui]] = 0;
    letterObj[letters[ui]]++
}
for(var ii = 0, cc = word.length; ii < cc; ii++) {



    var currentLetter = word[ii];
    if (letterObj[currentLetter] && letterObj[currentLetter] != 0)
        letterObj[currentLetter]--;
    else
        status = false;
    //document.writeln(currentLetter + letterObj[currentLetter])


}
document.writeln(words[i] + " " + status);

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment