Check whether second string can be formed from characters of first string used any number of times | C ++ implementation of the above approach ; Function to check if str2 can be made by characters of str1 or not ; To store the occurrence of every character ; Length of the two strings ; Assume that it is possible to compose the string str2 from str1 ; Iterate over str1 ; Store the presence of every character ; Iterate over str2 ; Ignore the spaces ; Check for the presence of character in str1 ; If it is possible to make str2 from str1 ; Driver Code ; Given strings ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isPossible ( string str1 , string str2 ) { int arr [ 256 ] = { 0 } ; int l1 = str1 . size ( ) ; int l2 = str2 . size ( ) ; int i , j ; bool possible = true ; for ( i = 0 ; i < l1 ; i ++ ) { arr [ str1 [ i ] ] = 1 ; } for ( i = 0 ; i < l2 ; i ++ ) { if ( str2 [ i ] != ' ▁ ' ) { if ( arr [ str2 [ i ] ] == 1 ) continue ; else { possible = false ; break ; } } } if ( possible ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } } int main ( ) { string str1 = " we ▁ all ▁ love ▁ geeksforgeeks " ; string str2 = " we ▁ all ▁ love ▁ geeks " ; isPossible ( str1 , str2 ) ; return 0 ; }