1

You are given a string find the frequency of all substring sorted(decreasing order) according to there frequency.

Eg: ababa {"a", "b", "a", "b", "a", "ab", "ba", "ab", "ba", "aba", "bab", "aba", "abab", "baba", "ababa"}.

output:

3,2,2,2,2,1,1,1,1

explanation

3 a 2 b 2 ba 2 aba 2 ab 1 abab 1 baba 1 ababa 1 bab

solution

1)one obvious solution is to keep all the string in hash map and count it frequency but it will take o(n^3logn) O(n^2 *n){n^2 number of substring *O(n) for comparision of string *logn(as map is maintened as Red black tree)} 2)Insert all substring in Ternary search tree then retrive frequency of each substring then sort the frequency O(n^3 logn)

I am wondering is there O(n^2) or O(nlogn) solution exsist.

like this http://www.quora.com/Given-a-string-how-do-I-find-the-number-of-distinct-substrings-of-the-string

nil96
  • 313
  • 1
  • 3
  • 12
  • possible duplicate of [How to count the number of ways of choosing of k equal substrings from a List L(the list of All Substrings)](http://stackoverflow.com/questions/30711272/how-to-count-the-number-of-ways-of-choosing-of-k-equal-substrings-from-a-list-l) – MBo Jun 10 '15 at 06:31

1 Answers1

1

An O(n^2) solution can be achieved in this way:

  1. Insert all substrings into a trie. This can be done in O(n^2).

  2. Obtain all frequency and sort them. Note that the frequency of any substring can only be in range [0, n], so a bucket sort can have all of the numbers sorted in O(n^2) since in the worst case there will be n^2 numbers.

lavin
  • 2,276
  • 2
  • 13
  • 15
  • There are O(n^2) substrings, of O(n) size. Inserting them to a trie is O(n^3). – amit Jun 09 '15 at 19:40
  • But substrings are incremental. so for "abc", inserting "a" "ab" "abc" only takes 3 operations on a trie, not 1+2+3 – lavin Jun 09 '15 at 19:42
  • Then it's not "insert O(n^2) substrings to a trie", it's something else if you want to use the fact that they are incremental. I am not claiming it cannot be done, I am claiming it's not black box insert O(n^2) substrings. – amit Jun 09 '15 at 19:42