Javascript Program to Check If a String is Substring of Another


In this problem, we need to ensure whether the string String2 contains string String1 as a substring.

We will first see the naïve approach to solving the problem, finding all substrings of String1’s length in String2 with String1. Furthermore, we will use built−in methods like match(), includes(), and indexOf() methods to find the String1 substring in the String2.

Problem statement − We have given a string String1 and String2 of different sizes. We need to check whether string String1 is present in string String2 as a substring.

Sample examples

Input

string1 = "tutor"; string2 = "pointtutorial";

Output

Yes

Explanation − It prints ‘yes’ as string1 exists in the string2 as a substring.

Input

 string1 = "cde"; string2 = "pointtutorial";

Output

No

Explanation − It prints No, as string1 doesn’t exist in string2.

Input

 string1 = "trsedf"; string2 = "rtel";

Output

No

Explanation − It prints No as the length of string2 is less than string1.

Approach 1

In this approach, we will find all substrings of length equal to string1’s length. After that, we will match it with the string1, and if they are equal, we can say that string2 contains the string1 as a substring.

Algorithm

Step 1 − Define the len1 and len2 variables and initialize them with string1 and string2’s length, respectively.

Step 2 − Start traversing the string2 from the 0th index of (len2 – len1)th index.

Step 3 − Use the nested loop, and make len1 iterations.

Step 4 − If string2[p + q] != string1[q] is true, break the loop.

Step 5 − After the loop iterations, if q == len1, the nested loop has made total len1 iterations as string1 exists as a substring. Return p.

Step 6 − Return −1 at last.

Example

Here the purpose of matching alpha2[p+q] with alpha[q] is matching the substring starting from the pth index in string2 with the string1.

function S1isSubOfS2(alpha1, alpha2) {
  // finding the length of strings
  var len1 = alpha1.length;
  var len2 = alpha2.length;
  //   Traverse the alpha2 to find alpha1
  for (var p = 0; p <= len2 - len1; p++) {
    var q;
    for (q = 0; q < len1; q++) {
      if (alpha2[p + q] != alpha1[q]) 
        break;
    }
    if (q == len1) return p;
  }
  return -1;
}
var string1 = "tutor";
var string2 = "pointtutorial";
if (S1isSubOfS2(string1, string2) != -1) {
  console.log("String1 is present in String2 as a substring.");
} else {
  console.log("String 1 is not present in String2 as a substring.");
}

Output

String1 is present in String2 as a substring.

Time complexity− O(len1*len2), as we traverse the string2 and find the substring of len1 starting from index p.

Space complexity − O(1) as we don’t use dynamic space.

Approach 2

In this approach, we will use JavaScript's built-in indexOf() method. The indexOf() method allows us to find the index of a particular character of a string in another string.

Example

In this example, we used the alpha2 string as a reference of the indexOf() method and passed the alpha1 string as a parameter. It returns −1 if alpha1 is not found in alpha2. Otherwise, it returns the index of the substring.

function S1isSubOfS2(alpha1, alpha2) {
  //  Using the indexOf() method
  return alpha2.indexOf(alpha1);
}
var string1 = "tutor";
var string2 = "pointtutorial";
if (S1isSubOfS2(string1, string2) != -1) {
  console.log("String1 is present in String2 as a substring.");
} else {
  console.log("String 1 is not present in String2 as a substring.");
}

Output

String1 is present in String2 as a substring.

Time complexity− O(n), which is the same as the time complexity of the indexOf() method.

Space complexity − O(1), which is the same as the indexOf() method’s space complexity.

Approach 3

In this approach, we will use the includes() method to check whether string2 contains string1. The includes() method returns true if string2 exists in the srting1. Otherwise, it returns false.

Example

In this example, we return 1 from the S1isSubOfS2() function if the includes() method returns true. Else, we return −1.

function S1isSubOfS2(alpha1, alpha2) {
  //  Using the includes() method
  if (alpha2.includes(alpha1)) {
    return 1;
  }
  return -1;
}
var string1 = "acd";
var string2 = "efadef";
if (S1isSubOfS2(string1, string2) != -1) {
  console.log("String1 is present in String2 as a substring.");
} else {
  console.log("String 1 is not present in String2 as a substring.");
}

Output

String 1 is not present in String2 as a substring.

Time complexity− O(n) is the same as includes() method’s time complexity.

Space complexity − O(1), which is equal to the includes() method’s space complexity.

Approach 4

In the fourth approach, we will use the match() method of JavaScript. The match() method is used to search for the specific pattern in the particular string and takes a string or regular expression as a parameter. It returns null if the match is not found. Otherwise, it returns an array containing all matches.

Example

In this example, we execute the match() method by passing the alpha1 string as a parameter. We return the matching index if the match is found. Otherwise, we return −1.

function S1isSubOfS2(alpha1, alpha2) {
  //  Using the match() method
  var result = alpha2.match(alpha1);
  if (result != null) {
    return result.index;
  }

  return -1;
}
var string1 = "acd";
var string2 = "efacdef";
if (S1isSubOfS2(string1, string2) != -1) {
  console.log("String1 is present in String2 as a substring.");
} else {
  console.log("String 1 is not present in String2 as a substring.");
}

Output

String1 is present in String2 as a substring.

Time complexity− O(n)

Space complexity − O(1)

We learned four approaches to solving the problem of finding string1 as a substring in string2. The first approach is the naïve approach which has more time complexity than other approaches. In JavaScript, it would be better to use the built−in methods as they are more optimized than the naïve approaches, and they can be useful to improve performance.

Updated on: 14-Aug-2023

134 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements