Skip to content
Open
Show file tree
Hide file tree
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
104 changes: 95 additions & 9 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,115 @@

from attr import has


def grouped_anagrams(strings):
""" This method will return an array of arrays.
"""
- Use a hash table to solve a coding problem
- Identify how a hash table can produce a more attractive runtime over alternative solutions
Given an array of strings, group anagrams together.
### Example
```
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
```
Note:
- All inputs will be in lowercase.
- The order of your output does not matter

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(m * nlogn) - | python sorting algorithm -> O(n. logn).
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

While this is an accurate answer assuming n stands for the number of characters in the strings and m stands for the number of strings in the list, we can make a simplifying assumption. Since we know the strings are English words, we can be pretty sure they will not get too big in size. On average words in English have about 5 letters, which is going to very quickly get dwarfed by the number of words in the list so we can simply say this is O(m) in time complexity.

Space Complexity: O(n)
"""
pass
# return empty list if no words
if len(strings) == 0:
return []

hash_table = {}
for item in strings:
# sort time complexity => O(n. logn).
string = "".join(sorted(item))
if string not in hash_table:
hash_table[string] = [item]
else:
hash_table[string].append(item)

print(hash_table.values())
return list(hash_table.values())


print(grouped_anagrams(["eat", "tae", "tea", "eta", "aet", "ate"]))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In general, make sure to remove testing code like this before you submit your PRs.


def top_k_frequent_elements(nums, k):
""" This method will return the k most common elements
"""
## Most Frequent K Elements
Given a non-empty array of integers, return the *k* most frequent elements.
## Example 1
```
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
```
## Example 2
```
Input: nums = [1], k = 1
Output: [1]
```
## Note

You may assume k is always valid, 1 ≤ k ≤ number of unique elements.

You should be able to equal or beat O(n log n), where n is the array's size.
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)
Space Complexity: O(n)
"""
pass

# if empty list return an empty array

if len(nums) == 0:
return []
# count the frequency of each element
hash_table = {}
frequency = [[] for i in range(len(nums) +1)]

for item in nums:
# coutn how many times an item occurs , return 0 if doesn't exists
hash_table[item] = 1 + hash_table.get(item,0)
# loop thorugh key-value pair
for item, count in hash_table.items():
# append to the the freq list
frequency[count].append(item)

result = []

# loop though all the way up until zero
# range(start, stop, step)
for index in range(len(frequency) -1, 0, -1):
for elem in frequency[index]:
result.append(elem)
if len(result) == k:
return result

print(top_k_frequent_elements([9,9,8,8,7],2))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same thing as above, make sure to remove testing code like this. It helps keep the code cleaner which is good style.


def valid_sudoku(table):
""" This method will return the true if the table is still
"""
OPTIONAL
This method will return the true if the table is still
a valid sudoku table.
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: ?
Space Complexity: ?
"""
# Optional QUESTION -
pass

2 changes: 1 addition & 1 deletion tests/test_grouped_anagrams.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_will_return_correct_result_for_readme_example():
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
]

# Assert
assert len(answer) == 3
Expand Down