Check if given words are present in a string | Find the small string at that index in the array of small strings is contained in the big string ; Function to the multiStringSearch ; iterate in the smallString ; calling the isInBigString Function ; Function to the bigString ; iterate in the bigString ; Check if length of smallString + i is greater than the length of bigString ; call the function isInBigStringHelper ; Helper Function to the Finding bigString ; Initialize leftBigIdx and rightBigIdx and leftSmallIdx variables ; Iterate until leftBigIdx variable reaches less than or equal to rightBigIdx ; Check if bigString [ leftBigIdx ] is not equal to smallString [ leftSmallIdx ] or Check if bigString [ rightBigIdx ] is not equal to smallString [ rightSmallIdx ] than return false otherwise increment leftBigIdx and leftSmallIdx decrement rightBigIdx and rightSmallIdx ; Driver code ; initialize string ; initialize vector string ; Function call ; Print answers ; Check if ans [ i ] is equal to 1 then Print true otherwise print false
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isInBigString ( string bigString , string smallString ) ; bool isInBigStringHelper ( string bigString , string smallString , int startIdx ) ; vector < bool > multiStringSearch ( string bigString , vector < string > smallStrings ) { vector < bool > solution ; for ( string smallString : smallStrings ) { solution . push_back ( isInBigString ( bigString , smallString ) ) ; } return solution ; } bool isInBigString ( string bigString , string smallString ) { for ( int i = 0 ; i < bigString . length ( ) ; i ++ ) { if ( i + smallString . length ( ) > bigString . length ( ) ) { break ; } if ( isInBigStringHelper ( bigString , smallString , i ) ) { return true ; } } return false ; } bool isInBigStringHelper ( string bigString , string smallString , int startIdx ) { int leftBigIdx = startIdx ; int rightBigIdx = startIdx + smallString . length ( ) - 1 ; int leftSmallIdx = 0 ; int rightSmallIdx = smallString . length ( ) - 1 ; while ( leftBigIdx <= rightBigIdx ) { if ( bigString [ leftBigIdx ] != smallString [ leftSmallIdx ] bigString [ rightBigIdx ] != smallString [ rightSmallIdx ] ) { return false ; } leftBigIdx ++ ; rightBigIdx -- ; leftSmallIdx ++ ; rightSmallIdx -- ; } return true ; } int main ( int argc , char * argv [ ] ) { string str = " this ▁ is ▁ a ▁ big ▁ string " ; vector < string > substr = { " this " , " yo " , " is " , " a " , " bigger " , " string " , " kappa " } ; vector < bool > ans = multiStringSearch ( str , substr ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { if ( ans [ i ] == 1 ) { cout << " true " << " ▁ " ; } else { cout << " false " << " ▁ " ; } } return 0 ; }