Created
January 22, 2020 17:32
-
-
Save SuperOleg39/b231db83c96ba80d19e6fe97a51b17bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const assert = require('assert'); | |
| /** | |
| * @param {Array<string>} anagrams | |
| * @returns Array<Array<string>> | |
| */ | |
| function groupAnagrams(anagrams) { | |
| const groups = new Map(); | |
| for (const word of anagrams) { | |
| const sortedWord = word.split('').sort().join(''); | |
| if (groups.has(sortedWord)) { | |
| groups.get(sortedWord).push(word); | |
| } else { | |
| groups.set(sortedWord, [word]); | |
| } | |
| } | |
| return Array.from(groups.values()); | |
| } | |
| const test = () => { | |
| (() => { | |
| assert.deepEqual( | |
| groupAnagrams([]), | |
| [] | |
| ); | |
| })(); | |
| (() => { | |
| assert.deepEqual( | |
| groupAnagrams([ | |
| "апельсин", | |
| "груша" | |
| ]), | |
| [ | |
| ["апельсин"], | |
| ["груша"] | |
| ] | |
| ); | |
| })(); | |
| (() => { | |
| assert.deepEqual( | |
| groupAnagrams([ | |
| "вертикаль", | |
| "кильватер", | |
| "апельсин", | |
| "спаниель", | |
| "австралопитек", | |
| "ватерполистка", | |
| "кластер", | |
| "сталкер", | |
| "стрелка", | |
| "корабль" | |
| ]), | |
| [ | |
| ["вертикаль", "кильватер"], | |
| ["апельсин", "спаниель"], | |
| ["австралопитек", "ватерполистка"], | |
| ["кластер", "сталкер", "стрелка"], | |
| ["корабль"] | |
| ] | |
| ); | |
| })(); | |
| }; | |
| test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment