Skip to content

191 - Anagram Groups - typescript #339

Open
@jsartisan

Description

@jsartisan

index.ts

export function groupAnagrams(strs: string[]): string[][] {
  const anagramMap = new Map<string, string[]>();

  for (let i = 0; i < strs.length; i++) {
    const current = strs[i];
    const sortedStr = current.split("").sort().join();

    if (anagramMap.has(sortedStr)) {
      anagramMap.get(sortedStr)!.push(current);
    } else {
      anagramMap.set(sortedStr, [current]);
    }
  }

  return Array.from(anagramMap.values());
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions