I need to check if an array is a sub-array of another bigger array. (find an word(sub-array) in a sentence(array)).
I need to do that in a recursion algorithm. that the run time will be log(n).
the arrays :
char[] sentence = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'};
char[] word = {'l', 'l', 'o', 'w', 'o', 'r'};
char [] word = {'t', 'n' , 'p'};
my code:
static boolean wordFinder(char[] arr, char[] arr2, int l, int i) {
if (i == arr2.length - 1) {
return true;
}
if (arr[l] == arr2[i]) {
return wordFinder(arr, arr2, l + 1, i + 1);
}
if (l == arr.length - 1) {
return false;
}
return wordFinder(arr, arr2, l + 1, 0);
}
the third array is only for checking the code. (the code works, just need to know the run time).