📌  相关文章
📜  检查数字中每个数字的频率是否等于其值

📅  最后修改于: 2022-05-13 01:56:08.849000             🧑  作者: Mango

检查数字中每个数字的频率是否等于其值

给定一个数字N ,任务是检查一个数字中每个数字的频率是否等于它的值
例子:

方法:可以通过将数字的频率存储在哈希图中,然后迭代哈希图以检查数字的频率是否等于其值来解决该任务。
下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if the number
// is valid or not
void check(int N)
{
    // Stores the frequencies
    // of digits
    unordered_map occ;
 
    while (N > 0) {
 
        // Extracting the digits
        int r = N % 10;
 
        // Incrementing the frequency
        occ[r]++;
 
        N /= 10;
    }
 
    // Iterating the hashmap
    for (auto i : occ) {
 
        // Check if frequency of digit
        // is equal to its value or not
        if (i.first != i.second) {
            cout << "No\n";
            return;
        }
    }
    cout << "Yes\n";
}
int main()
{
    int N = 3331;
    check(N);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.HashMap;
import java.util.Map;
 
class GFG
{
   
    // Function to check if the number
    // is valid or not
    public static void check(Integer N)
    {
       
        // Stores the frequencies
        // of digits
        HashMap occ = new HashMap<>();
 
        while (N > 0) {
 
            // Extracting the digits
            Integer r = N % 10;
            if (occ.containsKey(r)) {
 
                // If char is present in charCountMap,
                // incrementing it's count by 1
                occ.put(r, occ.get(r) + 1);
            }
            else {
 
                // If char is not present in charCountMap,
                // putting this char to charCountMap with 1
                // as it's value
                occ.put(r, 1);
            }
            N = N / 10;
        }
        for (Map.Entry e :
             occ.entrySet()) {
            if (e.getKey() != e.getValue()) {
                System.out.print("NO");
                return;
            }
        }
 
        System.out.print("Yes");
    }
   
  // Driver code
    public static void main(String[] args)
    {
 
        Integer N = 3331;
        check(N);
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python program for the above approach
 
# Function to check if the number
# is valid or not
def check(N):
   
  # Stores the frequencies
  # of digits
  occ = dict();
 
  while (N > 0):
 
    # Extracting the digits
    r = N % 10;
 
    # Incrementing the frequency
    if r in occ:
      occ[r] += 1
    else:
      occ[r] = 1
       
    N = N // 10
   
  # Iterating the hashmap
  for i in occ.keys():
 
    # Check if frequency of digit
    # is equal to its value or not
    if (i != occ[i]):
      print("No");
      return;
     
  print("Yes");
 
N = 3331;
check(N);
 
# This code is contributed by saurabh_jaiswal.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
   
    // Function to check if the number
    // is valid or not
    public static void check(int N)
    {
       
        // Stores the frequencies
        // of digits
        Dictionary occ = new Dictionary();
 
        while (N > 0) {
 
            // Extracting the digits
            int r = N % 10;
            if (occ.ContainsKey(r)) {
 
                // If char is present in charCountMap,
                // incrementing it's count by 1
                occ[r] = occ[r] + 1;
            }
            else {
 
                // If char is not present in charCountMap,
                // putting this char to charCountMap with 1
                // as it's value
                occ.Add(r, 1);
            }
            N = N / 10;
        }
        foreach(int key in occ.Keys) {
            if (key != occ[key]) {
                Console.Write("NO");
                return;
            }
        }
 
        Console.Write("Yes");
    }
   
  // Driver code
    public static void Main()
    {
 
        int N = 3331;
        check(N);
    }
}
 
// This code is contributed by Saurabh Jaiswal


Javascript


输出
Yes

时间复杂度:O(D), D = N 中的位数
辅助空间:O(D)