How to check for repeating sequence in an integer

后端 未结 5 1592
夕颜
夕颜 2021-01-31 18:15

I have an alpha-numeric string and I want to check for pattern repetition in it just for the integers. And they should be continuous.

Example

5条回答
  •  余生分开走
    2021-01-31 18:54

    My theory is that you can use the data structure known as suffix tree to achieve what you want.

    Going through the initial string, collect each contiguous sequence of digits and build its suffix tree. For your example it would look like (for the first 4 suffixes):

                      R - root
          |         |          |         |
          |         |          |         |
          |         |          |         | 
      12341234$  2341234$   341234$     41234$
    

    Now, the next suffix in order would be 1234$. However, when inserting, we notice that it matches the prefix 1234 of the first suffix. A counter is kept in parallel and incremented every time a suffix is added to the tree.

    At each step we compare the counter with the length of the match between the current suffix to be inserted and the substring with which it matches. If the length of the match is a multiple of the counter, then we have a repetition.

    In the above case, the counter would be 4 (starting from 0) by the time we insert 1234$ and the length of the match with the prefix of 12341234$ is also 4, so 1234 is repeated.

提交回复
热议问题