Check if two strings have a common substring | CPP program to heck if two strings have common substring ; function to return true if strings have common substring and no if strings have no common substring ; vector for storing character occurrences ; increment vector index for every character of str1 ; checking common substring of str2 in str1 ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; bool twoStrings ( string s1 , string s2 ) { vector < bool > v ( MAX_CHAR , 0 ) ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) v [ s1 [ i ] - ' a ' ] = true ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) if ( v [ s2 [ i ] - ' a ' ] ) return true ; return false ; } int main ( ) { string str1 = " hello " ; string str2 = " world " ; if ( twoStrings ( str1 , str2 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }