Strict Border Table

\( \def\sa#1{\tt{#1}} \def\dd{\dot{}\dot{}} \def\tbord{\mathit{border}} \def\sbord{\mathit{stbord}} \)

The strict-border table or best prefix table of a non-empty word $x$ is defined on the lengths $\ell$, $\ell=0, \dots, |x|$, of its prefixes by: $\sbord[0]=-1$, $\sbord[|x|]=\tbord[|x|]$ and, for $0 \lt \ell \lt |x|$, $\sbord[\ell]$ is the greatest $t$ satisfying

Word $x[0\dd \sbord[\ell]-1]$ is the strict border of prefix $x[0\dd \ell-1]$ of $x$. It exists only if $\sbord[\ell]\neq-1$.

Here are border and strict-border tables of the word $\sa{abaababaaba}$:
$i$ 012345678910
$x[i]$ $\sa{a}$$\sa{b}$$\sa{a}$$\sa{a}$$\sa{b}$$\sa{a}$$\sa{b}$$\sa{a}$$\sa{a}$$\sa{b}$$\sa{a}$
$\ell$ 01234567891011
$\tbord[\ell]$ -100112323456
$\sbord[\ell]$ -10-110-13-110-16

The strict-border table can be computed in $O(|x|)$ time by the following algorithm.

StrictBorder$(x \textrm{ non-empty word of length } m)$
    \begin{algorithmic}
   \STATE $stbord[0]\leftarrow -1$
   \STATE $i\leftarrow 0$
   \FOR{$j\leftarrow 1$ \TO $m-1$}
      \IF{$x[i] = x[j]$}
         \STATE $stbord[j]\leftarrow stbord[i]$
      \ELSE
         \STATE $stbord[j]\leftarrow i$
         \REPEAT
            \STATE $i\leftarrow stbord[i]$
         \UNTIL{$i \lt 0$ \OR $x[i] = x[j]$}
      \ENDIF
      \STATE $i\leftarrow i+1$
   \ENDFOR
   \STATE $stbord[m]\leftarrow i$
   \RETURN{$stbord$}
    \end{algorithmic}

The strict border table is used in the Knuth-Morris-Pratt exact string matching algorithm.

References

  • M. Crochemore, T. Lecroq, and W. Rytter. 125 Problems in Text Algorithms. Cambridge University Press, 2021.
  • D. E. Knuth, J. H. Morris Jr., and V. R. Pratt. Fast pattern matching in strings. SIAM J. Comput., 6(2):323-350, 1977.