Skip to content

Instantly share code, notes, and snippets.

@vlas-ilya
Created October 9, 2015 12:19
Show Gist options
  • Save vlas-ilya/f9dc6a7dcad81fa914bd to your computer and use it in GitHub Desktop.
Save vlas-ilya/f9dc6a7dcad81fa914bd to your computer and use it in GitHub Desktop.
var checkAnagram = function(word1, word2) {
if (word1 == "" || word2 == "") return false;
word1 = word1.split('').sort();
word2 = word2.split('').sort();
if (word1.length != word2.length) {
return false;
}
for (var i = 0; i < word1.length; i++) {
if (word1[i] != word2[i]) {
return false;
}
}
return true;
}
var anagramFinder = function (arr) {
var result = [];
var counter = 0;
for (var i = 0; i < arr.length - 1; i++) {
result[counter] = [arr[i]];
for (var j = i + 1; j < arr.length; j++) {
if (checkAnagram(arr[i], arr[j])) {
result[counter][result[counter].length] = arr[j];
arr[j] = "";
}
}
if (result[counter].length == 1) {
result[counter] = [];
} else {
counter += 1;
}
}
return result.filter(function (a) { return a.length > 0; })
.map(function (elem) { return elem.sort(); })
.sort();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment