Skip to content

Instantly share code, notes, and snippets.

@vinnymac
Last active March 17, 2016 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinnymac/87f5660ffe4f2e4c2e88 to your computer and use it in GitHub Desktop.
Save vinnymac/87f5660ffe4f2e4c2e88 to your computer and use it in GitHub Desktop.
Test whether or not two strings are permutations of each other.
arePermutations = (str1, str2) ->
return false unless str1.length is str2.length
occurrences = {}
for i in [0...str1.length]
char = str1.charAt(i)
occurrences[char] = (occurrences[char] or 0) + 1
for i in [0...str2.length]
char = str2.charAt(i)
return false unless occurrences[char]
occurrences[char] -= 1
return false if occurrences[char] < 0
return true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment