Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement