# Check if a string is substring of another

• Difficulty Level : Medium
• Last Updated : 24 Sep, 2021

Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1.

Examples :

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

```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"```

Simple Approach: The idea is to 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 run another loop checking for sub-string from every index.
For example, consider there to be a string of length N and a substring of length M. Then run a nested loop, where the outer loop runs from 0 to (N-M) and the inner loop from 0 to M. For very index check if the sub-string traversed by the inner loop is the given sub-string or not.

## C++

 `// C++ program to check if a string is``// substring of other.``#include ``using` `namespace` `std;` `// Returns true if s1 is substring of s2``int` `isSubstring(string s1, string s2)``{``    ``int` `M = s1.length();``    ``int` `N = s2.length();` `    ``/* A loop to slide pat[] one by one */``    ``for` `(``int` `i = 0; i <= N - M; i++) {``        ``int` `j;` `        ``/* For current index i, check for`` ``pattern match */``        ``for` `(j = 0; j < M; j++)``            ``if` `(s2[i + j] != s1[j])``                ``break``;` `        ``if` `(j == M)``            ``return` `i;``    ``}` `    ``return` `-1;``}` `/* Driver code */``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

 `// Java program to check if a string is``// substring of other.``class` `GFG {` `    ``// Returns true if s1 is substring of s2``    ``static` `int` `isSubstring(``        ``String s1, String s2)``    ``{``        ``int` `M = s1.length();``        ``int` `N = s2.length();` `        ``/* A loop to slide pat[] one by one */``        ``for` `(``int` `i = ``0``; i <= N - M; i++) {``            ``int` `j;` `            ``/* For current index i, check for``            ``pattern match */``            ``for` `(j = ``0``; j < M; j++)``                ``if` `(s2.charAt(i + j)``                    ``!= s1.charAt(j))``                    ``break``;` `            ``if` `(j == M)``                ``return` `i;``        ``}` `        ``return` `-``1``;``    ``}` `    ``/* Driver code */``    ``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);``    ``}``}` `// This code is contributed by JaideepPyne.`

## Python3

 `# Python3 program to check if``# a string is substring of other.` `# Returns true if s1 is substring of s2``def` `isSubstring(s1, s2):``    ``M ``=` `len``(s1)``    ``N ``=` `len``(s2)` `    ``# A loop to slide pat[] one by one``    ``for` `i ``in` `range``(N ``-` `M ``+` `1``):` `        ``# For current index i,``        ``# check for pattern match``        ``for` `j ``in` `range``(M):``            ``if` `(s2[i ``+` `j] !``=` `s1[j]):``                ``break``            ` `        ``if` `j ``+` `1` `=``=` `M :``            ``return` `i` `    ``return` `-``1` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``    ``s1 ``=` `"for"``    ``s2 ``=` `"geeksforgeeks"``    ``res ``=` `isSubstring(s1, s2)``    ``if` `res ``=``=` `-``1` `:``        ``print``(``"Not present"``)``    ``else``:``        ``print``(``"Present at index "` `+` `str``(res))` `# This code is contributed by ChitraNayal`

## C#

 `// C# program to check if a string is``// substring of other.``using` `System;``class` `GFG {` `    ``// Returns true if s1 is substring of s2``    ``static` `int` `isSubstring(``string` `s1, ``string` `s2)``    ``{``        ``int` `M = s1.Length;``        ``int` `N = s2.Length;` `        ``/* A loop to slide pat[] one by one */``        ``for` `(``int` `i = 0; i <= N - M; i++) {``            ``int` `j;` `            ``/* For current index i, check for``            ``pattern match */``            ``for` `(j = 0; j < M; j++)``                ``if` `(s2[i + j] != s1[j])``                    ``break``;` `            ``if` `(j == M)``                ``return` `i;``        ``}` `        ``return` `-1;``    ``}` `    ``/* Driver code */``    ``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);``    ``}``}` `// This code is contributed by nitin mittal.`

## PHP

 ``

## Javascript

 ``
Output
`Present at index 5`

Complexity Analysis:

• Time complexity: O(m * n) where m and n are lengths of s1 and s2 respectively.
A nested loop is used the outer loop runs from 0 to N-M and inner loop from 0 to M so the complexity is O(m*n).
• Space Complexity: O(1).
As no extra space is required.

An efficient solution is to use a O(n) searching algorithm like KMP algorithm, Z algorithm, etc.
Language implementations:

Another Efficient Solution:

• An efficient solution would need only one traversal i.e. O(n) on the longer string s1. Here we will start traversing the string s1 and maintain a pointer for string s2 from 0th index.
• For each iteration we compare the current character in s1 and check it with the pointer at s2.
• If they match we increment the pointer on s2 by 1. And for every mismatch we set the pointer back to 0.
• Also keep a check when the s2 pointer value is equal to the length of string s2, if true we break and return the value (pointer of string s1 – pointer of string s2)
• Works with strings containing duplicate characters.

## C++

 `#include ``using` `namespace` `std;` `int` `Substr(string s2, string s1)``{``    ``int` `counter = 0; ``// pointing s2``    ``int` `i = 0;``    ``for``(;i 0)``            ``{``                ``i -= counter;``            ``}``            ``counter = 0;``        ``}``    ``}``    ``return` `counter < s2.length()?-1:i-counter;``}` `// Driver code``int` `main()``{``    ``string s1 = ``"geeksfffffoorrfoorforgeeks"``;``    ``cout << Substr(``"for"``, s1);``    ``return` `0;``}` `// this code is contributed by Manu Pathria`

## Java

 `import` `java.io.*;` `class` `GFG {``   ``public` `static` `int` `Substr(String s2, String s1){``        ``int` `counter = ``0``; ``//pointing s2``        ``int` `i = ``0``;``        ``for``(;i``0``){``                    ``i -= counter;``                ``}``                ``counter = ``0``;``            ``}``        ``}``        ``return` `counter < s2.length()?-``1``:i-counter;``    ``}``    ``public` `static` `void` `main (String[] args) {``        ``String s1 = ``"geeksfffffoorrfoorforgeeks"``;``        ``//System.out.println(s2.indexOf("for"));``        ``System.out.println(Substr(``"for"``, s1));``    ``}``}`

## Python3

 `# Python program for the above approach``def` `Substr(``Str``, target):``    ` `    ``t ``=` `0``    ``Len` `=` `len``(``Str``)``    ``i ``=` `0``    ` `    ``# Iterate from 0 to Len - 1``    ``for` `i ``in` `range``(``Len``):``        ``if` `(t ``=``=` `len``(target)):``            ``break``        ``if` `(``Str``[i] ``=``=` `target[t]):``            ``t ``+``=` `1``        ``else``:``            ``t ``=` `0``            ` `    ``if` `(t < ``len``(target)):``        ``return` `-``1``    ``else``:``        ``return` `(i ``-` `t)` `# Driver code``print``(Substr(``"GeeksForGeeks"``, ``"Fr"``))``print``(Substr(``"GeeksForGeeks"``, ``"For"``))` `# This code is contributed by avanitrachhadiya2155`

## Javascript

 `` `` ``
Output
`18`

Complexity Analysis:

The complexity of the above code will be still O(n*m) in the worst case and the space complexity is O(1).

My Personal Notes arrow_drop_up