Checks if a string is an anagram of another string in Dart- isAnagram


Tue Apr 28 2020 19:52:02 GMT+0000 (UTC)

Saved by @toast-ghost #dart

bool isAnagram(String str1, String str2) {
  String normalize(String str) => (str
          .replaceAll(RegExp(r'[^a-z0-9]', caseSensitive: false), '')
  return normalize(str1) == normalize(str2);

Use String.toLowerCase() and String.prototype.replaceAll() with an appropriate regular expression to remove unnecessary characters and convert the string to lowercase. Use String.split(''), Iterable.sort(), in combination with the cascade operator (..) and Iterable.join('') to normalize both strings and check if their normalized forms are equal.