diff --git a/lib/exercises.js b/lib/exercises.js index 8036868..96c08cb 100644 --- a/lib/exercises.js +++ b/lib/exercises.js @@ -1,17 +1,60 @@ // This method will return an array of arrays. // Each subarray will have strings which are anagrams of each other -// Time Complexity: ? -// Space Complexity: ? +// Time Complexity: O(n * m log m) - N: number of strings passed in; M: sorting time of each string +// Space Complexity: O(n * m) - N: number of unique letter sets; M: number of total arrays stored function grouped_anagrams(strings) { - throw new Error("Method hasn't been implemented yet!"); + let tracker = {} + + for (let i = 0; i < strings.length; i++) { + let word = strings[i].split("").sort(); + + tracker[word] ? tracker[word].push(strings[i]) : tracker[word] = [strings[i]] + } + + const keys = Object.keys(tracker); + + const result = keys.map(key => { return tracker[key] }); + return result } // 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: ? +// Time Complexity: O(n + m) -> O(n) - N: length of list; m: unique numbers +// Space Complexity: O(m) m: unique numbers entered in hash function top_k_frequent_elements(list, k) { - throw new Error("Method hasn't been implemented yet!"); + if (list.length === 0) { + return [] + } + + let tracker = {}; + + for (let i = 0; i < list.length; i++) { + if (tracker[list[i]]) { + tracker[list[i]]++; + } else { + tracker[list[i]] = 1; + } + } + + + let result = []; + + for (let i = 0; i < k; i++) { + let max = 0; + let maxKey = 0; + + for (let key in tracker) { + if (tracker[key] > max) { + max = tracker[key] + maxKey = key; + delete tracker[key] + } + } + + result.push(Number(maxKey)) + } + + return result } @@ -20,14 +63,72 @@ function top_k_frequent_elements(list, k) { // Each element can either be a ".", or a digit 1-9 // The same digit cannot appear twice or more in the same // row, column or 3x3 subgrid -// Time Complexity: ? +// Time Complexity: O(n*m) - n: rows in sudoku table; m: columns in sudoku table // Space Complexity: ? + function valid_sudoku(table) { - throw new Error("Method hasn't been implemented yet!"); -} + let tracker = { + row: {}, + column: {}, + table: {} + }; + + for (let i = 0; i < table.length; i++) { + for (let j = 0; j < table.length; j++) { + const value = table[i][j] + if (value === '.') { + continue + } + const tableIndex = Math.floor((i / 3)) * 3 + j / 3 + + // tracker.table[tableIndex] = tracker.table[tableIndex] ? tracker.table[tableIndex] : {}; + // tracker.row[i] = tracker.row[i] ? tracker.row[i] : {}; + // tracker.column[j] = tracker.column[j] ? tracker.column[j] : {}; + if (!tracker.table[tableIndex]) { + tracker.table[tableIndex] = {}; + } + if (!tracker.row[i]) { + tracker.row[i]= {}; + } + if (!tracker.column[j]) { + tracker.column[j] = {}; + } + + if (tracker.table[tableIndex][value] || tracker.row[i][value] || tracker.column[j][value]) { + return false; + } + + tracker.table[tableIndex][value] = true; + tracker.row[i][value] = true; + tracker.column[j][value] = true; + } + } + + // console.log(tracker) + return true; +}; module.exports = { grouped_anagrams, top_k_frequent_elements, valid_sudoku }; + + +// console.log(grouped_anagrams(["eat", "tea", "tan", "ate", "nat", "bat"])); +// console.log(grouped_anagrams([])); +// console.log(grouped_anagrams(["eat", "ear", "tar", "pop", "pan", "pap"])); +// console.log(grouped_anagrams(["eat", "tae", "tea", "eta", "aet", "ate"])); +// console.log(grouped_anagrams(["tea","","eat","","tea",""])); +// console.log(top_k_frequent_elements([1,1,1,2,2,3], 2)) +// console.log(top_k_frequent_elements([], 1)) +console.log(valid_sudoku([ + [".","8","7","6","5","4","3","2","1"], + ["2",".",".",".",".",".",".",".","."], + ["3",".",".",".",".",".",".",".","."], + ["4",".",".",".",".",".",".",".","."], + ["5",".",".",".",".",".",".",".","."], + ["6",".",".",".",".",".",".",".","."], + ["7",".",".",".",".",".",".",".","."], + ["8",".",".",".",".",".",".",".","."], + ["9",".",".",".",".",".",".",".","."]])) \ No newline at end of file diff --git a/test/exercises.test.js b/test/exercises.test.js index 3de85e6..24cc6f6 100644 --- a/test/exercises.test.js +++ b/test/exercises.test.js @@ -9,9 +9,10 @@ describe("exercises", function () { it("will return [] for an empty array", function () { // Arrange const list = []; - + + // Act-Assert - expect(grouped_anagrams(list)).to.eql([]); + expect(grouped_anagrams(list)).toEqual([]); }); it("will work for the README example", function () { @@ -27,9 +28,9 @@ describe("exercises", function () { ]; // Assert - expect(answer.length).to.be.greaterThan(0); + expect(answer.length).toBeGreaterThan(0); answer.forEach((array, index) => { - expect(array.sort()).to.eql(expected_answer[index]); + expect(array.sort()).toEqual(expected_answer[index]); }); }); @@ -50,9 +51,9 @@ describe("exercises", function () { ]; // Assert - expect(answer.length).to.be.greaterThan(0); + expect(answer.length).toBeGreaterThan(0); answer.forEach((array) => { - expect(expected_answer).to.deep.include(array.sort()); + expect(expected_answer).toContainEqual(array.sort()); }); }); @@ -67,14 +68,14 @@ describe("exercises", function () { ]; // Assert - expect(answer.length).to.be.greaterThan(0); + expect(answer.length).toBeGreaterThan(0); answer.forEach((array) => { - expect(expected_answer).to.deep.include(array.sort()); + expect(expected_answer).toContainEqual(array.sort()); }); }); }); - describe.skip("top_k_frequent_elements", function () { + describe("top_k_frequent_elements", function () { it("works with example 1", function () { // Arrange const list = [1, 1, 1, 2, 2, 3]; @@ -84,7 +85,7 @@ describe("exercises", function () { const answer = top_k_frequent_elements(list, k); // Assert - expect(answer.sort()).to.eql([1, 2]); + expect(answer.sort()).toEqual([1, 2]); }); it("works with example 2", function () { @@ -96,7 +97,7 @@ describe("exercises", function () { const answer = top_k_frequent_elements(list, k); // Assert - expect(answer.sort()).to.eql([1]); + expect(answer.sort()).toEqual([1]); }); it("will return [] for an empty array", function () { @@ -108,7 +109,7 @@ describe("exercises", function () { const answer = top_k_frequent_elements(list, k); // Assert - expect(answer.sort()).to.eql([]); + expect(answer.sort()).toEqual([]); }); it("will work for an array with k elements all unique", function () { @@ -120,7 +121,7 @@ describe("exercises", function () { const answer = top_k_frequent_elements(list, k); // Assert - expect(answer.sort()).to.eql([1, 2, 3]); + expect(answer.sort()).toEqual([1, 2, 3]); }); it("will work for an array when k is 1 and several elements appear 1 time (HINT Pick the 1st one)", function () { @@ -132,11 +133,11 @@ describe("exercises", function () { const answer = top_k_frequent_elements(list, k); // Assert - expect(answer.sort()).to.eql([1]); + expect(answer.sort()).toEqual([1]); }); }); - describe.skip("valid sudoku", function () { + describe("valid sudoku", function () { it("is not valid if a row has duplicate values", function () { // Arrange const table = [ @@ -155,7 +156,7 @@ describe("exercises", function () { const valid = valid_sudoku(table); // Assert - expect(valid).to.be.false; + expect(valid).toEqual.false; }); it("is not valid if a column has duplicate values", function () { @@ -176,7 +177,7 @@ describe("exercises", function () { const valid = valid_sudoku(table); // Assert - expect(valid).to.be.false; + expect(valid).toEqual.false; }); it("works for the table given in the README", function () { @@ -197,7 +198,7 @@ describe("exercises", function () { const valid = valid_sudoku(table); // Assert - expect(valid).to.be.true; + expect(valid).toEqual.true; }); it("fails for the table given in the README", function () { @@ -218,7 +219,7 @@ describe("exercises", function () { const valid = valid_sudoku(table); // Assert - expect(valid).to.be.false; + expect(valid).toEqual.false; }); it("fails for a duplicate number in a sub-box", function () { @@ -239,7 +240,7 @@ describe("exercises", function () { const valid = valid_sudoku(table); // Assert - expect(valid).to.be.false; + expect(valid).toEqual.false; }); it("fails for a duplicate number in a bottom right sub-box", function () { @@ -260,7 +261,8 @@ describe("exercises", function () { const valid = valid_sudoku(table); // Assert - expect(valid).to.be.false; + expect(valid).toEqual.false; }); }); }); +