Here\'s a very simple way to build an suffix array from a string in python:
def sort_offsets(a, b):
return cmp(content[a:], content[b:])
content = \"foobar
I don't know if there's a fast way to compare substrings, but you can make your code much faster (and simpler) by using key
instead of cmp
:
suffix_array.sort(key=lambda a: content[a:])
This will create the substring just once for each value of a.
Edit: A possible downside is that it will require O(n^2) memory for the substrings.