Anagrams
Given an array of strings, return all groups of strings that are anagrams.
Example
Given [“lint”, “intl”, “inlt”, “code”], return [“lint”, “inlt”, “intl”].
Given [“ab”, “ba”, “cd”, “dc”, “e”], return [“ab”, “ba”, “cd”, “dc”].
Note
All inputs will be in lower-case
Solution
- sorted_string: A list of sorted strings matching the input
strs
For example:
1 2 |
|
hash_map :
<sorted_string[i], NumofAppearance(sorted_string[i])>
maintain how many times does each sorted string appear.- Sort each string from strs and put it into sorted_string
- Use the hash map to record how many times does sorted_string[i] appear.
- Chose the string which appear more than 2 times. Put it into result string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|