text: Check if frequency of character in one string is a factor or multiple of frequency of same character in other string | C ++ implementation of above approach ; Function that checks if the frequency of character are a factor or multiple of each other ; map store frequency of each character ; if any frequency is 0 , then continue as condition is satisfied ; if factor or multiple , then condition satisfied ; if condition not satisfied ; Driver code
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; bool multipleOrFactor ( string s1 , string s2 ) { map < char , int > m1 , m2 ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) m1 [ s1 [ i ] ] ++ ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) m2 [ s2 [ i ] ] ++ ; map < char , int > :: iterator it ; for ( it = m1 . begin ( ) ; it != m1 . end ( ) ; it ++ ) { if ( m2 . find ( ( * it ) . first ) == m2 . end ( ) ) continue ; if ( m2 [ ( * it ) . first ] % ( * it ) . second == 0 || ( * it ) . second % m2 [ ( * it ) . first ] == 0 ) continue ; else return false ; } } int main ( ) { string s1 = " geeksforgeeks " ; string s2 = " geeks " ; multipleOrFactor ( s1 , s2 ) ? cout << " YES " : cout << " NO " ; return 0 ; }