Number of substrings of one string present in other | CPP program to count number of substrings of s1 present in s2 . ; s3 stores all substrings of s1 ; check the presence of s3 in s2 ; Driver code
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; int countSubstrs ( string s1 , string s2 ) { int ans = 0 ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { string s3 ; for ( int j = i ; j < s1 . length ( ) ; j ++ ) { s3 += s1 [ j ] ; if ( s2 . find ( s3 ) != string :: npos ) ans ++ ; } } return ans ; } int main ( ) { string s1 = " aab " , s2 = " aaaab " ; cout << countSubstrs ( s1 , s2 ) ; return 0 ; }