Created
December 23, 2014 04:58
-
-
Save krg7880/1026b89e134c42bdb3c3 to your computer and use it in GitHub Desktop.
Javascript Anagram Algorithm
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
| /** | |
| Slightly modified version | |
| of the anagram found on Stackoverflow. | |
| @see: http://stackoverflow.com/a/16577324/1707987 | |
| - Replaced str.substr with str.charAt | |
| - Cached the length of the first string | |
| - Check the cached len against match counter | |
| */ | |
| function anagram(a, b) { | |
| if (typeof a === 'string' && typeof b === 'string' && a === b) { | |
| return true; | |
| } | |
| if (a.length !== b.length) { | |
| return false; | |
| } | |
| var c = null; | |
| var i = 0; | |
| var n = a.length; | |
| var matches = 0; | |
| var idx = 0; | |
| while(i < n) { | |
| c = a.charAt( (i += 1) ); | |
| idx = b.indexOf(c); | |
| if (idx < 0) { | |
| return false; | |
| } | |
| s2 = b.substr(0, idx) + b.substr( (idx += 1) ); | |
| matches += 1; | |
| } | |
| return matches === n; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment