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.
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.
Present at index 5
- 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.
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.
- Check if the given string is shuffled substring of another string
- Find length of longest subsequence of one string which is substring of another string
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Length of longest substring to be deleted to make a string equal to another string
- 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
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can be formed from another string using given constraints
- Check if a string can be repeated to make another string
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a string can be obtained by rotating another string d places
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if a string is a scrambled form of another string
- Queries to check if string B exists as substring in string A
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Check if a string is suffix of another
- Check if it is possible to convert one string into another with given constraints
- Check if it is possible to transform one string to another
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.