Check if a two character string can be made using given words | CPP code to check if a two character string can be made using given strings ; Function to check if str can be made using given words ; If str itself is present ; Match first character of str with second of word and vice versa ; If both characters found . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool makeAndCheckString ( vector < string > words , string str ) { int n = words . size ( ) ; bool first = false , second = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( words [ i ] == str ) return true ; if ( str [ 0 ] == words [ i ] [ 1 ] ) first = true ; if ( str [ 1 ] == words [ i ] [ 0 ] ) second = true ; if ( first && second ) return true ; } return false ; } int main ( ) { string str = " ya " ; vector < string > words = { " ah " , " oy " , " to " , " ha " } ; if ( makeAndCheckString ( words , str ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }