Check if a pair of strings exists that starts with and without the character K or not | C ++ program for the above approach ; Function to check whether a pair of strings exists satisfying the conditions ; Stores the visited strings ; Iterate over the array arr [ ] ; If first character of current string is K ; Otherwise ; Adding to the visited ; Driver Code ; Given Input
#include <bits/stdc++.h> NEW_LINE using namespace std ; string checkhappy ( vector < string > arr , char K , int N ) { set < string > visited ; for ( string s : arr ) { if ( s [ 0 ] == K ) if ( visited . find ( s . substr ( 1 ) ) != visited . end ( ) ) return " Yes " ; else if ( visited . find ( ( K + s ) ) != visited . end ( ) ) return " Yes " ; visited . insert ( s ) ; } return " No " ; } int main ( ) { vector < string > arr = { " a " , " ! ▁ a " , " b " , " ! ▁ c " , " d " , " ! ▁ d " } ; char K = ' ! ' ; int N = arr . size ( ) ; cout << checkhappy ( arr , K , N ) << endl ; return 0 ; }