2

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
  • 1
    You need to add a return statement at the very end or at least have one for every conditional branch because at the moment your code is reaching a branch with no return statement. – ophact Dec 21 '21 at 05:15
  • You could also use `omgi.split( omg )` If you end up with more than one piece, you have at least one match. – Michel Dec 21 '21 at 05:48

2 Answers2

1

From what I understand, you're just remaking String.prototype.match. Try checking this out, as it would probably be an easier way to do what you're saying. Sorry, missed the O(N) complexity part.

If you really wanna make a custom one, I can give you some tips.
First of all, you should have a "cache" variable (one for all, not left and right) and another variable which will be found (it'll be a boolean, so set it to false). The "cache" variable will store the text, and the other one will store whether you found the smallString.

Basically, you loop through every character and store however long smallString characters in the "cache" variable. Once the "cache" variable is the same length as smallString, run an if statement on it. If it's not equal to the smallString, remove the first character of the "cache". Next iteration in the loop, it'll add another character. You then do the same as before, run an if statement and if it's not equal remove the first character and continue the loop until you find it, or the string ends. If you found it, set the boolean to true.

Something like this:

function stringSearch(smallString, bigString, caseSensitive=true) {
    if(!caseSensitive) { // if caseSensitive is false, make everything lower case
        smallString = smallString.toLowerCase();
        bigString = bigString.toLowerCase();
    }
    
    let cache = ""; // string cache
    let found = false; // result

    for(i=0;i<bigString.length;i++) { // loop through every character in bigString
        cache+=bigString[i]; // add the current character to the cache
        if(cache.length == smallString.length) { // check if the cache's length is the same as the smallString's length
            if(cache == smallString) { // check if the cache is equal to the smallString
                found = true; // set found to true
                break; // break the loop (stop it from going on)
            } else {
                cache = cache.substring(1); // set cache to itself but remove the first character
            }
        }
    }

    return found; // return result
}


// example:

console.log("String 'hello, world' has 'hello': "+stringSearch("hello", "hello, world"));
console.log("String 'HELLO WORLD' has 'hello': "+stringSearch("hello", "HELLO WORLD"));
console.log("String 'HELLO WORLD' has 'hello' (not case sensitive): "+stringSearch("hello", "HELLO WORLD", false));
console.log("String 'hey hi hello WORLD' has 'hello': "+stringSearch("hello", "hey hi hello WORLD"));
console.log("String 'hey hi hello' has 'hello': "+stringSearch("hello", "hey hi hello"));
console.log("String 'hey lol o' has 'hello': "+stringSearch("hello", "hey lol o"));
Battledash2
  • 100
  • 1
  • 8
0

I see a couple of issues. First, the code doesn't have a return statement for every logical branch. I mean that, for every condition in an if, then, else statement, the code should have a return statement or some sort of control-flow statement (e.g. a recursive call or a continue) that will eventually lead to a return statement.

The second issue is the while loop. Supposedly, right should be greater than left from the beginning of the function (unless the length of smallString is 0) because right is the length of smallerString minus 1. The while condition is left > right, so nothing inside of the while will be executed unless smallString has a negative length (which it doesn't).

By the way, if you want to check the entire bigString, you will need to iterate over bigString, not smallString. If you are only checking smallString.length characters, you won't be checking the entire bigString. Figuring out how to write this function is a good exercise, so I will leave the writing of it to you and refrain from providing an implementation myself. Keep up the good work!

tedtanner
  • 577
  • 6
  • 19