Last active
December 14, 2015 11:38
-
-
Save hiroshi-maybe/5080144 to your computer and use it in GitHub Desktop.
Extract suffix array from string.
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
| var suffix_array = (function(str){ | |
| var create_suffixes = function(arg_str) { | |
| var suffixes = []; | |
| for (var i=0, length=arg_str.length; i<length; i+=1) { | |
| var work=""; | |
| for (var j=i; j<length; j+=1) { | |
| work+=arg_str.charAt(j); | |
| } | |
| suffixes[i] = {string:work, index:i}; | |
| } | |
| return suffixes; | |
| }; | |
| return function(str) { | |
| var suffixes_array=[]; | |
| var suffixes = create_suffixes(str); | |
| suffixes.sort(function(a,b) { | |
| return a.string>b.string ? 1 : -1; | |
| }); | |
| for (var i=0, length=str.length; i<length; i+=1) { | |
| suffixes_array[i]=suffixes[i].index; | |
| } | |
| return suffixes_array; | |
| }; | |
| })(str); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment