Skip to content

Instantly share code, notes, and snippets.

@Quingsley
Created May 12, 2024 04:26
Show Gist options
  • Save Quingsley/c372ffedb87ac7cc91087863a93c04ae to your computer and use it in GitHub Desktop.
Save Quingsley/c372ffedb87ac7cc91087863a93c04ae to your computer and use it in GitHub Desktop.
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