Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate all unique substrings for given string

Given a string s, what is the fastest method to generate a set of all its unique substrings?

Example: for str = "aba" we would get substrs={"a", "b", "ab", "ba", "aba"}.

The naive algorithm would be to traverse the entire string generating substrings in length 1..n in each iteration, yielding an O(n^2) upper bound.

Is a better bound possible?

(this is technically homework, so pointers-only are welcome as well)

like image 403
Yuval Adam Avatar asked Apr 01 '10 12:04

Yuval Adam


People also ask

How do you get all substrings of a given length of a string in Java?

Find the position of all matches for the string using the regex \w(? =\w\w) . This will give you the start index of the first character of each required sub-string. In this case, you would get: 0 , 1 , 2 , 3 , 4 , 8 , 9 , 10 and 11 .


1 Answers

As other posters have said, there are potentially O(n^2) substrings for a given string, so printing them out cannot be done faster than that. However there exists an efficient representation of the set that can be constructed in linear time: the suffix tree.

like image 174
Rafał Dowgird Avatar answered Oct 02 '22 06:10

Rafał Dowgird