Check if a string is substring of another
Last Updated :
08 May, 2023
Given two strings S1 and S2, The task is to find if S1 is a substring of S2. If yes, return the index of the first occurrence, else return -1.
Examples :
Input: S1 = “for”, S2= “geeksforgeeks”
Output: 5
Explanation: String “for” is present as a substring of s2.
Input: S1 = “practice”, S2= “geeksforgeeks”
Output: -1.
Explanation: There is no occurrence of “practice” in “geeksforgeeks”
Naive Approach: Below is the idea to solve the problem.
Run a loop from start to end and for every index in the given string check whether the sub-string can be formed from that index. This can be done by running a nested loop traversing the given string and in that loop running another loop checking for sub-strings starting from every index.
Follow the steps below to implement the idea:
- Run a for loop with counter i from 0 to N – M.
- Run a for loop with counter j from 0 to M-1.
- Compare jth character of S1 with (i+j)th character of S2.
- If the loop terminates after matching all the characters, then return i, i.e. substring S1 is found starting from ith character of S2
- Return -1 as no substring is found.
Below is the Implementation of the above idea.
C
#include <stdio.h>
#include <string.h>
int isSubstring( char * s1, char * s2)
{
int M = strlen (s1);
int N = strlen (s2);
for ( int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (s2[i + j] != s1[j])
break ;
if (j == M)
return i;
}
return -1;
}
int main()
{
char s1[] = "for" ;
char s2[] = "geeksforgeeks" ;
int res = isSubstring(s1, s2);
if (res == -1)
printf ( "Not present" );
else
printf ( "Present at index %d" , res);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
int isSubstring(string s1, string s2)
{
int M = s1.length();
int N = s2.length();
for ( int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (s2[i + j] != s1[j])
break ;
if (j == M)
return i;
}
return -1;
}
int main()
{
string s1 = "for" ;
string s2 = "geeksforgeeks" ;
int res = isSubstring(s1, s2);
if (res == -1)
cout << "Not present" ;
else
cout << "Present at index " << res;
return 0;
}
|
Java
class GFG {
static int isSubstring(String s1, String s2)
{
int M = s1.length();
int N = s2.length();
for ( int i = 0 ; i <= N - M; i++) {
int j;
for (j = 0 ; j < M; j++)
if (s2.charAt(i + j) != s1.charAt(j))
break ;
if (j == M)
return i;
}
return - 1 ;
}
public static void main(String args[])
{
String s1 = "for" ;
String s2 = "geeksforgeeks" ;
int res = isSubstring(s1, s2);
if (res == - 1 )
System.out.println( "Not present" );
else
System.out.println( "Present at index " + res);
}
}
|
Python3
def isSubstring(s1, s2):
M = len (s1)
N = len (s2)
for i in range (N - M + 1 ):
for j in range (M):
if (s2[i + j] ! = s1[j]):
break
if j + 1 = = M:
return i
return - 1
if __name__ = = "__main__" :
s1 = "for"
s2 = "geeksforgeeks"
res = isSubstring(s1, s2)
if res = = - 1 :
print ( "Not present" )
else :
print ( "Present at index " + str (res))
|
C#
using System;
class GFG {
static int isSubstring( string s1, string s2)
{
int M = s1.Length;
int N = s2.Length;
for ( int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (s2[i + j] != s1[j])
break ;
if (j == M)
return i;
}
return -1;
}
public static void Main()
{
string s1 = "for" ;
string s2 = "geeksforgeeks" ;
int res = isSubstring(s1, s2);
if (res == -1)
Console.Write( "Not present" );
else
Console.Write( "Present at index " + res);
}
}
|
PHP
<?php
function isSubstring( $s1 , $s2 )
{
$M = strlen ( $s1 );
$N = strlen ( $s2 );
for ( $i = 0; $i <= $N - $M ; $i ++)
{
$j = 0;
for (; $j < $M ; $j ++)
if ( $s2 [ $i + $j ] != $s1 [ $j ])
break ;
if ( $j == $M )
return $i ;
}
return -1;
}
$s1 = "for" ;
$s2 = "geeksforgeeks" ;
$res = isSubstring( $s1 , $s2 );
if ( $res == -1)
echo "Not present" ;
else
echo "Present at index " . $res ;
?>
|
Javascript
<script>
function isSubstring(s1, s2)
{
var M = s1.length;
var N = s2.length;
for ( var i = 0; i <= N - M; i++) {
var j;
for (j = 0; j < M; j++)
if (s2[i + j] != s1[j])
break ;
if (j == M)
return i;
}
return -1;
}
var s1 = "for" ;
var s2 = "geeksforgeeks" ;
var res = isSubstring(s1, s2);
if (res == -1)
document.write( "Not present" );
else
document.write( "Present at index " + res);
</script>
|
Output
Present at index 5
Time complexity: O(M * N) where m and n are lengths of s1 and s2 respectively, Nested loops are used, outer from 0 to N – M and inner from 0 to M
Auxiliary Space: O(1), As no extra space is required.
Check if a string is a substring of another using STL:
std::find from C++ STL, the index method in Python, the indexOf method in Java, the indexOf method in JavaScript are some inbuilt functions in the libraries of respective languages for finding the starting index of a substring in a given string.
Below is the Implementation of above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int isSubstring(string s1, string s2)
{
if (s2.find(s1) != string::npos)
return s2.find(s1);
return -1;
}
int main()
{
string s1 = "for" ;
string s2 = "geeksforgeeks" ;
int res = isSubstring(s1, s2);
if (res == -1)
cout << "Not present" ;
else
cout << "Present at index " << res;
return 0;
}
|
Python3
def isSubstring(s1, s2):
if s1 in s2:
return s2.index(s1)
return - 1
if __name__ = = "__main__" :
s1 = "for"
s2 = "geeksforgeeks"
res = isSubstring(s1, s2)
if res = = - 1 :
print ( "Not present" )
else :
print ( "Present at index " + str (res))
|
Java
class GFG {
static int isSubstring(String s1, String s2)
{
return s2.indexOf(s1);
}
public static void main(String[] args)
{
String s1 = "for" ;
String s2 = "geeksforgeeks" ;
int res = isSubstring(s1, s2);
if (res == - 1 )
System.out.println( "Not present" );
else
System.out.println( "Present at index " + res);
}
}
|
C#
using System;
public class GFG {
static int isSubstring( string s1, string s2)
{
return s2.IndexOf(s1);
}
public static void Main( string [] args)
{
string s1 = "for" ;
string s2 = "geeksforgeeks" ;
int res = isSubstring(s1, s2);
if (res == -1)
Console.WriteLine( "Not present" );
else
Console.WriteLine( "Present at index " + res);
}
}
|
Javascript
function isSubstring(s1, s2)
{
return s2.indexOf(s1);
}
var s1 = "for" ;
var s2 = "geeksforgeeks" ;
var res = isSubstring(s1, s2);
if (res == -1)
console.log( "Not present" );
else
console.log( "Present at index " + res);
|
Output
Present at index 5
Time Complexity: O(N) , where N is the length of the longer string s2
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...