# Check if frequency of character in one string is a factor or multiple of frequency of same character in other string

Given two strings, the task is to check whether the frequencies of a character(for each character) in one string is a multiple or a factor in another string. If it is, then output “YES”, otherwise output “NO”.

**Examples:**

Input:s1 = “aabccd”, s2 = “bbbaaaacc”

Output:YES

Frequency of ‘a’ in s1 and s2 are 2 and 4 respectively, and 2 is a factor of 4

Frequency of ‘b’ in s1 and s2 are 1 and 3 respectively, and 1 is a factor of 3

Frequency of ‘c’ in s1 and s2 are same hence it also satisfies.

Frequency of ‘d’ in s1 and s2 are 1 and 0 respectively, but 0 is a multiple of every number, hence satisfied.

Hence, the answer YES.

Input:s1 = “hhdwjwqq”, s2 = “qwjdddhhh”

Output:NO

**Approach:**

- Store frequency of characters in s1 in first map STL.
- Store frequency of characters in s2 in second map STL.
- Let the frequency of a character in first map be F1. Let us also assume the frequency of this character in second map is F2.
- Check F1%F2 and F2%F1(modulo operation). If either of them is 0, then the condition is satisfied.
- Check it for all the characters.

Below is the implementation of the above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that checks if the frequency of character ` `// are a factor or multiple of each other ` `bool` `multipleOrFactor(string s1, string s2) ` `{ ` ` ` `// map store frequency of each character ` ` ` `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 any frequency is 0, then continue ` ` ` `// as condition is satisfied ` ` ` `if` `(m2.find((*it).first) == m2.end()) ` ` ` `continue` `; ` ` ` ` ` `// if factor or multiple, then condition satified ` ` ` `if` `(m2[(*it).first] % (*it).second == 0 ` ` ` `|| (*it).second % m2[(*it).first] == 0) ` ` ` `continue` `; ` ` ` ` ` `// if condition not satisfied ` ` ` `else` ` ` `return` `false` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string s1 = ` `"geeksforgeeks"` `; ` ` ` `string s2 = ` `"geeks"` `; ` ` ` ` ` `multipleOrFactor(s1, s2) ? cout << ` `"YES"` ` ` `: cout << ` `"NO"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of above approach ` `from` `collections ` `import` `defaultdict ` ` ` `# Function that checks if the frequency of ` `# character are a factor or multiple of each other ` `def` `multipleOrFactor(s1, s2): ` ` ` ` ` `# map store frequency of each character ` ` ` `m1 ` `=` `defaultdict(` `lambda` `:` `0` `) ` ` ` `m2 ` `=` `defaultdict(` `lambda` `:` `0` `) ` ` ` `for` `i ` `in` `range` `(` `0` `, ` `len` `(s1)): ` ` ` `m1[s1[i]] ` `+` `=` `1` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, ` `len` `(s2)): ` ` ` `m2[s2[i]] ` `+` `=` `1` ` ` ` ` `for` `it ` `in` `m1: ` ` ` ` ` `# if any frequency is 0, then continue ` ` ` `# as condition is satisfied ` ` ` `if` `it ` `not` `in` `m2: ` ` ` `continue` ` ` ` ` `# if factor or multiple, then condition satified ` ` ` `if` `(m2[it] ` `%` `m1[it] ` `=` `=` `0` `or` ` ` `m1[it] ` `%` `m2[it] ` `=` `=` `0` `): ` ` ` `continue` ` ` ` ` `# if condition not satisfied ` ` ` `else` `: ` ` ` `return` `False` ` ` ` ` `return` `True` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `s1 ` `=` `"geeksforgeeks"` ` ` `s2 ` `=` `"geeks"` ` ` ` ` `if` `multipleOrFactor(s1, s2): ` `print` `(` `"YES"` `) ` ` ` `else` `: ` `print` `(` `"NO"` `) ` ` ` `# This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

**Output:**

YES

## Recommended Posts:

- Check if the frequency of any character is more than half the length of the string
- Longest sub-string having frequency of each character less than equal to k
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Check if a string has all characters with same frequency with one variation allowed
- Print number of words, vowels and frequency of each character
- Check whether frequency of characters in a string makes Fibonacci Sequence
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
- Check if both halves of the string have at least one different character
- Check whether the Average Character of the String is present or not
- Check if a two character string can be made using given words
- Python | Check order of character in string using OrderedDict( )
- Check if max occurring character of one string appears same no. of times in other
- Replace every character of a string by a different character
- Frequency of a substring in a string

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.