JS Built-ins
Algorithmsbeginner

Anagram Check

πŸ“– Usage Example
function isAnagram(a, b) {
  const sort = s => s.split('').sort().join('');
  return sort(a) === sort(b);
}
isAnagram('listen', 'silent'); // true

Check if two strings are anagrams by sorting their characters and comparing.

Anagram Check

Two strings are anagrams if they contain the same characters in a different order (ignoring case and spaces).

"listen" and "silent" are anagrams
"hello" and "world" are not anagrams

Algorithm:

  1. Normalize both strings (lowercase, remove spaces)
  2. Sort characters in both strings
  3. Compare the sorted strings
function isAnagram(str1: string, str2: string): boolean {
  // Return true if str1 and str2 are anagrams
}

isAnagram('listen', 'silent') // true
πŸ’‘ Hints (5)
  • β€’ Convert both strings to lowercase for case-insensitive comparison.
  • β€’ Use split("") to convert string to array of characters.
  • β€’ Use sort() to sort the characters.
  • β€’ Use join("") to convert back to string.
  • β€’ Compare the two sorted strings.