Skip to content

183 - Anagrams - typescript #187

Open
@jsartisan

Description

@jsartisan

index.ts

export function areAnagrams(s: string, t: string): boolean {
  if (s.length !== t.length) {
    return false;
  }

  const sFreq: Map<string, number> = new Map();
  const tFreq: Map<string, number> = new Map();

  for (let i = 0; i < s.length; i++) {
    sFreq.set(s[i], (sFreq.get(s[i]) || 0) + 1);
    tFreq.set(t[i], (tFreq.get(t[i]) || 0) + 1);
  }

  for (const [char, count] of sFreq) {
    if (tFreq.get(char) !== count) {
      return false;
    }
  }

  return true;
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions