To find all the repeating substring in a given string

后端 未结 3 1288
感动是毒
感动是毒 2021-02-01 23:38

I recetly come across an interview question : To find all the repeating substring in a given string with a minimal size of 2. The algorithm should be efficient one.

Cod

3条回答
  •  难免孤独
    2021-02-02 00:12

    If you analyze the output for the string "AAAAAAAAAAAAAA", then there are O(n²) characters in it, so the algorithm is at least O(n²).

    To achieve O(n²), just build the suffix tree for each suffix of s (indices [1..n], [2..n], [3..n], ..., [n..n]). It doesn't matter if one of the strings has no own end node, just count how often each node is used.

    At the end, iterate over each node with count>1 and print its path.

提交回复
热议问题