Skip to content

Instantly share code, notes, and snippets.

@hiroshi-maybe
Last active December 14, 2015 11:38
Show Gist options
  • Select an option

  • Save hiroshi-maybe/5080144 to your computer and use it in GitHub Desktop.

Select an option

Save hiroshi-maybe/5080144 to your computer and use it in GitHub Desktop.
Extract suffix array from string.
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