17 Jun 2023

Check if Two Strings are Anagrammatic

Taken from LeetCode

Given two strings s and t, return true if t is an anagram of s, and false otherwise.


This is the answer I came up with:

function isAnagram(s: string, t: string): boolean {
  // An anagram isn't possible if the lengths differ
  if (s.length !== t.length) { return false }

  // Convert strings to sorted arrays
  let _s = [...s].sort()
  let _t = [...t].sort()
  // Stringify the arrays and compare them
  return JSON.stringify(_s) === JSON.stringify(_t)


Solution tested in REPL:


Copyright © Paramdeo Singh · All Rights Reserved · Built with Jekyll

This node last updated November 7, 2023 and is permanently morphing...

Paramdeo Singh Guyana

Generalist. Edgerunner. Riding the wave of consciousness in this treacherous mortal sea.

Technology Design Strategy Literature Personal Blogs
Search Site

Results are from Blog, Link Dumps, and #99Problems