Given two strings s1 and s2, find if s1 is substring of s2. If yes, return index of first occurrence, else return -1.
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.
Present at index 5
Time complexity : O(m * n) where m and n are lengths of s1 and s2 respectively.
Language implementations :
- 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
- Queries to check if substring[L...R] is palindrome or not
- Check if two strings have a common substring
- Suffix Tree Application 1 - Substring Check
- Frequency of a substring in a string
- First substring whose reverse is a word in the string
- Frequency of a substring in a string using pthread
- 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 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.