Open In App

Python Program To Check If A String Is Substring Of Another

Last Updated : 20 Jan, 2022
Comments
Improve
Suggest changes
1 Like
Like
Report

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 : 

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. 

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.

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).

Please refer complete article on Check if a string is substring of another for more details!

Next Article

Similar Reads