Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions lib/exercises.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
// 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 ^ 2) since the array being sorted has a length less than 10, the sort() method uses InsertionSort.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you also have a number of words, so you need to consider the array length as well.

If the words are small you could elect to consider sorting the words as a constant since it's pretty fast, but you definitely need to consider the number of strings.

// If the array.length was greater than 10, QuickSort would be implemented and the time complexity would be O(nlog(n)) because I have to loop through the array to put it in the hash table.
// Source: https://blog.shovonhasan.com/time-space-complexity-of-array-sort-in-v8/
// Space Complexity: O(n), with n being the number of strings in the array.
function grouped_anagrams(strings) {
throw new Error("Method hasn't been implemented yet!");
if (strings.length === 0) {
return []
}
const words_hash = {};

strings.forEach((word) => {
// alphabetize word
const sorted_word = word.split('').sort().join();
if (words_hash[sorted_word]) {
words_hash[sorted_word].push(word);
} else {
words_hash[sorted_word] = [word];
}
})

return Object.values(words_hash);

}


// This method will return the k most common elements
// in the case of a tie it will select the first occuring element.
// Time Complexity: ?
Expand Down