Speed 1x

1/19
ABBBABA
ABA
for i in range(N - M + 1):
    j = 0
    while(j < M):
        if (txt[i + j] != pat[j]):
            break
        j += 1
    if (j == M): 
        return i

  • j
    Needle comparison pointer
    0
  • i
    Needle offset pointer
    0
  • M
    Length of the haystack
    7
  • N
    length of the needle
    3
  • pat
    Needle
    ABA
  • txt
    Haystack
    ABBBABA
Step: 1 of 19

Moving the needle offset pointer along to the next position in the haystack.