diff --git a/lib/exercises.js b/lib/exercises.js index 648394e..19af4c2 100644 --- a/lib/exercises.js +++ b/lib/exercises.js @@ -1,16 +1,35 @@ // This method will return an array of arrays. // Each subarray will have strings which are anagrams of each other -// Time Complexity: ? -// Space Complexity: ? -function grouped_anagrams(strings) { - throw new Error("Method hasn't been implemented yet!"); +// Time Complexity: O(n) where n is the length of the input array +// Space Complexity: O(nm) where n is the length of the input array and m is the average length of the values in the array +function groupedAnagrams(strings) { + const hashTable = {}; + + if (strings == false) { + return []; + } + + strings.forEach(function(string) { + const arrayString = string.split(''); + const sorted = arrayString.sort(); + const sortedString = sorted.join(); + + if (hashTable[sortedString] == undefined) { + hashTable[sortedString] = [string] + } else { + hashTable[sortedString].push(string) + } + }); + + const output = Object.values(hashTable) + return output } // This method will return the k most common elements // in the case of a tie it will select the first occuring element. // Time Complexity: ? // Space Complexity: ? -function top_k_frequent_elements(list, k) { +function topKFrequentElements(list, k) { throw new Error("Method hasn't been implemented yet!"); } @@ -22,12 +41,12 @@ function top_k_frequent_elements(list, k) { // row, column or 3x3 subgrid // Time Complexity: ? // Space Complexity: ? -function valid_sudoku(table) { +function validSudoku(table) { throw new Error("Method hasn't been implemented yet!"); } module.exports = { - grouped_anagrams, - top_k_frequent_elements, - valid_sudoku + groupedAnagrams, + topKFrequentElements, + validSudoku }; diff --git a/test/exercises.test.js b/test/exercises.test.js index 93461ab..3b544bc 100644 --- a/test/exercises.test.js +++ b/test/exercises.test.js @@ -1,18 +1,18 @@ const expect = require('chai').expect; const { - grouped_anagrams, - top_k_frequent_elements, - valid_sudoku + groupedAnagrams, + topKFrequentElements, + validSudoku } = require('../lib/exercises'); describe("exercises", function() { - describe("grouped_anagrams", function() { + describe("groupedAnagrams", function() { it("will return [] for an empty array", function() { // Arrange const list = []; // Act-Assert - expect(grouped_anagrams(list)).to.eql([]); + expect(groupedAnagrams(list)).to.eql([]); }); it("will work for the README example", function() { @@ -20,7 +20,7 @@ describe("exercises", function() { const list = ["eat", "tea", "tan", "ate", "nat", "bat"]; // Act - const answer = grouped_anagrams(list); + const answer = groupedAnagrams(list); const expected_answer = [ ["ate","eat","tea"], ["nat","tan"], @@ -39,7 +39,7 @@ describe("exercises", function() { const list = ["eat", "ear", "tar", "pop", "pan", "pap"]; // Act - const answer = grouped_anagrams(list); + const answer = groupedAnagrams(list); const expected_answer = [ ["eat"], @@ -62,7 +62,7 @@ describe("exercises", function() { const list = ["eat", "tae", "tea", "eta", "aet", "ate"] // Act - const answer = grouped_anagrams(list); + const answer = groupedAnagrams(list); const expected_answer = [ [ "aet", "ate", "eat", "eta", "tae", "tea"] ]; @@ -75,14 +75,14 @@ describe("exercises", function() { }); }); - describe.skip("top_k_frequent_elements", function() { + describe.skip("topKFrequentElements", function() { it("works with example 1", function() { // Arrange const list = [1,1,1,2,2,3]; const k = 2; // Act - const answer = top_k_frequent_elements(list, k); + const answer = topKFrequentElements(list, k); // Assert expect(answer.sort()).to.eql([1,2]); @@ -94,7 +94,7 @@ describe("exercises", function() { const k = 1; // Act - const answer = top_k_frequent_elements(list, k); + const answer = topKFrequentElements(list, k); // Assert expect(answer.sort()).to.eql([1]); @@ -106,7 +106,7 @@ describe("exercises", function() { const k = 1; // Act - const answer = top_k_frequent_elements(list, k); + const answer = topKFrequentElements(list, k); // Assert expect(answer.sort()).to.eql([]); @@ -118,7 +118,7 @@ describe("exercises", function() { const k = 3; // Act - const answer = top_k_frequent_elements(list, k); + const answer = topKFrequentElements(list, k); // Assert expect(answer.sort()).to.eql([1, 2, 3]); @@ -130,7 +130,7 @@ describe("exercises", function() { const k = 1; // Act - const answer = top_k_frequent_elements(list, k); + const answer = topKFrequentElements(list, k); // Assert expect(answer.sort()).to.eql([1]); @@ -153,7 +153,7 @@ describe("exercises", function() { ]; // Act - const valid = valid_sudoku(table); + const valid = validSudoku(table); // Assert expect(valid).toEqual(true); @@ -174,7 +174,7 @@ describe("exercises", function() { ]; // Act - const valid = valid_sudoku(table); + const valid = validSudoku(table); // Assert expect(valid).toEqual(false); @@ -195,7 +195,7 @@ describe("exercises", function() { ]; // Act - const valid = valid_sudoku(table); + const valid = validSudoku(table); // Assert expect(valid).toEqual(false); @@ -216,7 +216,7 @@ describe("exercises", function() { ]; // Act - const valid = valid_sudoku(table); + const valid = validSudoku(table); // Assert expect(valid).toEqual(false);