Exercise 19: Check if String is Anagram

Problem Statement

Function `isAnagram(str1, str2)` banao jo check kare ki dono strings anagram hain ya nahi. Example: isAnagram("listen", "silent") => true

Sample Output:

isAnagram("listen", "silent") => true
isAnagram("hello", "world") => false

Solution

const isAnagram = (str1, str2) => {
  const normalize = (s) => s.toLowerCase().replace(/[^a-z]/g, "").split("").sort().join("");
  return normalize(str1) === normalize(str2);
};

Explanation

Overall Goal:

  • Dono strings anagram hain ya nahi check karna.

Real world:

  • Word games: anagram detection.
  • String algorithms: pattern matching.