I'm trying to find the substring in string using O(N) complexity. The following is the code I have written. It returns undefined and I don't know why. Please let me know what is going wrong in my code.
let omg = "omg";
let omgi = "omjiklmonomgib";
function stringSearch(smallString, bigString) {
let left = 0;
let right = left+(smallString.length - 1);
while (left > right) {
if (smallString[left] === bigString[left]) {
if (smallString[right] === bigString[right]) {
left++;
right--;
if (left === right) {
if (smallString[right] === bigString[left]) {
return true;
} else if (right === left + 1) {
if (smallString[right] === bigString[right] && smallString[left] === bigString[left]) {
return true;
} else {
left = right + 1;
right = left + (smallString.length - 1);
}
}
}
}
}
}
}
console.log(stringSearch(omg, omgi)); //Undefined