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”.
Input: s1 = “aabccd”, s2 = “bbbaaaacc”
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”
- 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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if max occurring character of one string appears same no. of times in other
- Sub-strings that start and end with one character and have at least one other
- Check if a string has all characters with same frequency with one variation allowed
- Map every character of one string to another such that all occurrences are mapped to the same character
- Check if two strings can be made equal by swapping one character among each other
- Check if frequency of all characters can become same by one removal
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Count pairs in an array such that frequency of one is at least value of other
- Length of array pair formed where one contains all distinct elements and other all same elements
- Rearrange the given string such that all prime multiple indexes have same character
- Check if characters of one string can be swapped to form other
- Check if one string can be converted to other using given operation
- NFA to accept strings that has atleast one character occurring in a multiple of 3
- Nearest element with at-least one common prime factor
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Shortest distance to every other character from given character
- Check whether one root of the Quadratic Equation is twice of other or not
- Pair of integers with difference K having an element as the K-th multiple of the other
- Meta Strings (Check if two strings can become same after a swap in one string)
- Check if the frequency of all the digits in a number is same
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.