VOOZH about

URL: https://www.geeksforgeeks.org/dsa/number-of-substrings-of-one-string-present-in-other/

⇱ Number of substrings of one string present in other - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Number of substrings of one string present in other

Last Updated : 7 Oct, 2022

Suppose we are given a string s1, we need to the find total number of substring(including multiple occurrences of the same substring) of s1 which are present in string s2. 

Examples: 

Input : s1 = aab
 s2 = aaaab
Output :6
Substrings of s1 are ["a", "a", "b", "aa", 
"ab", "aab"]. These all are present in s2. 
Hence, answer is 6.

Input :s1 = abcd
 s2 = swalencud
Output :3 

The idea is to consider all substrings of s1 and check if it present in s2.  

Implementation:


Output
6

Complexity Analysis:

  • Time Complexity: O(n*n*n), as nested loops are used where n is the size of string s1
  • Auxiliary Space: O(n), as extra space for string s3 is being used
Comment