Medium Problems

49. Group Anagrams

Solutions

function groupAnagrams(strs: string[]): string[][] {
  const map = {};
 
  for(let i = 0; i < strs.length; i++) {
     let str = strs[i]
     let sorted = str.split('').sort().join('')
      if(map[sorted] === undefined) {
          map[sorted] = [str]
      } else {
          map[sorted].push(str) 
      }
  }
 
  let res = [];
  for(let key in map) {
      res.push(map[key])
  }
  return res;
};

Video Solution

...good question to practice fundamental

Last Update: 11:43 - 16 April 2024
Array
Hash Table
String
Sorting

On this page