What algorithm can you use to find duplicate phrases in a string?

前端 未结 5 1165
广开言路
广开言路 2020-12-15 11:26

Given an arbitrary string, what is an efficient method of finding duplicate phrases? We can say that phrases must be longer than a certain length to be included.

Id

5条回答
  •  情话喂你
    2020-12-15 12:23

    suppose you are given sorted array A with n entries (i=1,2,3,...,n)

    Algo(A(i))
    {
      while i<>n
      {
        temp=A[i];
        if A[i]<>A[i+1] then
        {     
          temp=A[i+1];
          i=i+1;
          Algo(A[i])
        }
        else if A[i]==A[i+1] then
          mark A[i] and A[i+1] as duplicates
      }
    }
    

    This algo runs at O(n) time.

提交回复
热议问题