📌  相关文章
📜  检查一个字符串中字符的频率是否是另一字符串中相同字符的频率的因数或倍数

📅  最后修改于: 2021-04-22 06:28:40             🧑  作者: Mango

给定两个字符串,任务是检查在一个字符串中的字符(每个字符)的频率是否是多还是在另一个字符串中的一个因素。如果是,则输出“是”,否则输出“否”。

例子:

方法:

  1. 将字符频率存储在第一个映射STL中的s1中。
  2. 将字符频率存储在第二个地图STL中的s2中。
  3. 令第一个图中的字符频率为F1。让我们还假设此字符在第二个映射中的出现频率为F2。
  4. 检查F1%F2和F2%F1(模运算)。如果它们中的任何一个为0,则满足条件。
  5. 检查所有字符。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
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 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::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;
}


Java
// Java implementation of above approach
import 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 m1 = new HashMap<>();
        HashMap 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 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 satified
            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 = "geeksforgeeks", s2 = "geeks";
        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 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


C#
// C# implementation of the approach
using 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 m1 = new Dictionary();
        Dictionary m2 = new Dictionary();
  
        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 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 satified
            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 = "geeksforgeeks", s2 = "geeks";
        if (multipleOrFactor(s1, s2))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed by PrinciRaj1992


输出:
YES