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 are 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 the first map be F1. Let us also assume the frequency of this character in the 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 otherbool 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 satisfied if (m2[(*it).first] % (*it).second == 0 || (*it).second % m2[(*it).first] == 0) continue; // if condition not satisfied else return false; }}// Driver codeint main(){ string s1 = "zambiatek"; string s2 = "zambiatek"; multipleOrFactor(s1, s2) ? cout << "YES" : cout << "NO"; return 0;} |
Java
// Java implementation of above approachimport java.util.HashMap;class GFG { // Function that checks if the frequency of character // are a factor or multiple of each other public static boolean multipleOrFactor(String s1, String s2) { // map store frequency of each character HashMap<Character, Integer> m1 = new HashMap<>(); HashMap<Character, Integer> m2 = new HashMap<>(); for (int i = 0; i < s1.length(); i++) { if (m1.containsKey(s1.charAt(i))) { int x = m1.get(s1.charAt(i)); m1.put(s1.charAt(i), ++x); } else m1.put(s1.charAt(i), 1); } for (int i = 0; i < s2.length(); i++) { if (m2.containsKey(s2.charAt(i))) { int x = m2.get(s2.charAt(i)); m2.put(s2.charAt(i), ++x); } else m2.put(s2.charAt(i), 1); } for (HashMap.Entry<Character, Integer> entry : m1.entrySet()) { // if any frequency is 0, then continue // as condition is satisfied if (!m2.containsKey(entry.getKey())) continue; // if factor or multiple, then condition satisfied if (m2.get(entry.getKey()) != null && (m2.get(entry.getKey()) % entry.getValue() == 0 || entry.getValue() % m2.get(entry.getKey()) == 0)) continue; // if condition not satisfied else return false; } return true; } // Driver code public static void main(String[] args) { String s1 = "zambiatek", s2 = "zambiatek"; if (multipleOrFactor(s1, s2)) System.out.println("Yes"); else System.out.println("No"); }}// This code is contributed by// sanjeev2552 |
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 satisfied 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 = "zambiatek" s2 = "zambiatek" if multipleOrFactor(s1, s2): print("YES") else: print("NO") # This code is contributed by Rituraj Jain |
C#
// C# implementation of the approachusing System;using System.Collections.Generic;class GFG { // Function that checks if the // frequency of character are // a factor or multiple of each other public static Boolean multipleOrFactor(String s1, String s2) { // map store frequency of each character Dictionary<char, int> m1 = new Dictionary<char, int>(); Dictionary<char, int> m2 = new Dictionary<char, int>(); for (int i = 0; i < s1.Length; i++) { if (m1.ContainsKey(s1[i])) { var x = m1[s1[i]]; m1[s1[i]]= ++x; } else m1.Add(s1[i], 1); } for (int i = 0; i < s2.Length; i++) { if (m2.ContainsKey(s2[i])) { var x = m2[s2[i]]; m2[s2[i]]= ++x; } else m2.Add(s2[i], 1); } foreach(KeyValuePair<char, int> entry in m1) { // if any frequency is 0, then continue // as condition is satisfied if (!m2.ContainsKey(entry.Key)) continue; // if factor or multiple, then condition satisfied if (m2[entry.Key] != 0 && (m2[entry.Key] % entry.Value == 0 || entry.Value % m2[entry.Key] == 0)) continue; // if condition not satisfied else return false; } return true; } // Driver code public static void Main(String[] args) { String s1 = "zambiatek", s2 = "zambiatek"; if (multipleOrFactor(s1, s2)) Console.WriteLine("Yes"); else Console.WriteLine("No"); }}// This code is contributed by PrinciRaj1992 |
Javascript
<script>// JavaScript implementation of above approach// Function that checks if the frequency of character// are a factor or multiple of each otherfunction multipleOrFactor(s1, s2){ // map store frequency of each character let m1 = new Map(); let m2 = new Map(); for (let i = 0; i < s1.length; i++){ if(m1[s1[i]]) m1[s1[i]]++; else m1[s1[i]] = 1 } for (let i = 0; i < s2.length; i++){ if(m2[s2[i]]) m2[s2[i]]++; else m2[s2[i]] = 1 } for (var it in m1) { // if any frequency is 0, then continue // as condition is satisfied if (!(m2[it])) continue; // if factor or multiple, then condition satisfied if (m2[it] % m1[it] == 0 || m1[it] % m2[it] == 0) continue; // if condition not satisfied else return false; } return true;}// Driver codelet s1 = "zambiatek";let s2 = "zambiatek";multipleOrFactor(s1, s2) ?document.write( "YES") : document.write("NO"); </script> |
YES
Complexity Analysis:
- Time Complexity: O((n+m)*log(n+m)), where n is the size of string s1 and m is the size of string s2
- Auxiliary Space: O(n)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



