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 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
- Largest index for each distinct character in given string with frequency K
- Index of character depending on frequency count in string
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Check if frequency of each character is equal to its position in English Alphabet
- Rearrange the given string such that all prime multiple indexes have same character
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Print the frequency of each character in Alphabetical order
- 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
- Minimum addition/removal of characters to be done to make frequency of each character prime
- Modify the string such that every character gets replaced with the next character in the keyboard
- 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
- Replace all occurrences of character X with character Y in given string
- Map every character of one string to another such that all occurrences are mapped to the same character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.