49. Группировка анаграмм (Medium) (https://leetcode.com/problems/group-anagrams/)
Пример 1: Ввод: [‘eat’,‘tea’,‘tan’,‘ate’,‘nat’,‘bat’] Вывод: [[‘bat’],[‘nat’,‘tan’],[‘eat’,‘tea’,‘ate’]] Объяснение: … Пример 2: Ввод: [”] Вывод: ” Объяснение: … Пример 3: Ввод: [‘a’] Вывод: ‘a’ Объяснение: …
function groupAnagrams(strs: string[]): string[][] {
if (strs.length === 1) return [strs]
const group = new Map<string, string[]>()
for (const ch of strs) {
const curr = ch.split("").sort().join("")
const currVal: string[] | undefined = group.get(curr)
if (currVal === undefined) {
group.set(curr, [ch])
} else {
currVal.push(ch)
group.set(curr, currVal)
}
}
return Array.from(group.values())
}
// by charCode [O(n)]
// function groupAnagrams(strs: string[]): string[][] {
// if (strs.length === 1) return [strs]
// const mp = new Map<string, string[]>()
// for (const ch of strs) {
// const count = new Array(26).fill(0)
// for (const c of ch) {
// count[c.charCodeAt(0) - 97]++
// }
// const countStr = count.join('#')
// const currVal = mp.get(countStr)
// if (currVal === undefined) {
// mp.set(countStr, [ch])
// } else {
// currVal.push(ch)
// mp.set(countStr, currVal)
// }
// }
// return Array.from(mp.values())
// }
// Local check:
console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]))
console.log(groupAnagrams([""]))
console.log(groupAnagrams(["a"]))Example 1:
Input: ['eat','tea','tan','ate','nat','bat']
Output: [['bat'],['nat','tan'],['eat','tea','ate']]
Explanation: ...
Example 2:
Input: ['']
Output: [['']]
Explanation: ...
Example 3:
Input: ['a']
Output: [['a']]
Explanation: ...