Created
May 12, 2024 04:26
-
-
Save Jerome-Jumah/c372ffedb87ac7cc91087863a93c04ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
bool result = isAnagram('iceman', 'cinemas'); | |
print(result); | |
} | |
bool isAnagram(String first, String second) { | |
if (first.length != second.length) { | |
return false; | |
} | |
var lookUp = <String, int>{}; | |
// (n + n ) = O(2n) = O(n) | |
// n | |
for (final c in first.split('')) { | |
lookUp[c] = (lookUp[c] ?? 0) + 1; | |
} | |
// n | |
for (final c in second.split('')) { | |
if (lookUp[c] == null) { | |
return false; | |
} else { | |
lookUp[c] = lookUp[c]! - 1; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment