Check if a string is substring of another
Given two strings s1 and s2, find if s1 is substring of s2. If yes, return index of first occurrence, else return -1.
Examples :
Input : s1 = "for", s2 = "geeksforgeeks" Output : 5 String "for" is present as a substring of s2. Input : s1 = "practice", s2 = "geeksforgeeks" Output : -1.
A simple solution is to one by one check every index of s2. For every index, check if s1 is present.
C++
// CPP program to check if a string is // substring of other. #include <bits/stdc++.h> 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 program to test above function */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 program to test above function */ 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. |
Python 3
# Python 3 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 program to test above function */ 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
<?php // PHP program to check if a // string is substring of other. // Returns true if s1 is substring of s2 function isSubstring($s1, $s2) { $M = strlen($s1); $N = strlen($s2); // A loop to slide // pat[] one by one for ($i = 0; $i <= $N - $M; $i++) { $j = 0; // For current index i, // check for pattern match for (; $j < $M; $j++) if ($s2[$i + $j] != $s1[$j]) break; if ($j == $M) return $i; } return -1; } // Driver Code $s1 = "for"; $s2 = "geeksforgeeks"; $res = isSubstring($s1, $s2); if ($res == -1) echo "Not present"; else echo "Present at index " . $res; // This code is contributed by mits ?> |
Present at index 5
Time complexity : O(m * n) where m and n are lengths of s1 and s2 respectively.
An efficient solution is to use a O(n) searching algorithm like KMP algorithm, Z algorithm, etc.
Language implementations :
Recommended Posts:
- Queries to check if string B exists as substring in string A
- Python | Check if a Substring is Present in a Given String
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Check if string can be made lexicographically smaller by reversing any substring
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Find length of longest subsequence of one string which is substring of another string
- Minimum changes required to make first string substring of second string
- Check if two strings have a common substring
- Queries to check if substring[L...R] is palindrome or not
- Suffix Tree Application 1 - Substring Check
- Frequency of a substring in a string
- Frequency of a substring in a string using pthread
- Lexicographical Maximum substring of string
- Shortest substring of a string containing all given words
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



