Search code examples
javascriptarraysstringalgorithmsubstring

Finding subString in String


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


Solution

  • 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"));