📜  两个给定数字中存在的公共数字的数量

📅  最后修改于: 2021-10-27 08:44:16             🧑  作者: Mango

给定两个正数NM ,任务是计算NM中出现的数字的数量。

例子:

方法:可以使用Hashing解决给定的问题。请按照以下步骤解决问题:

  • 初始化一个变量,比如count0 ,以存储两个数字中共有的位数。
  • 初始化两个数组,例如freq1[10]freq2[10]{0} ,以分别存储整数NM 中存在的数字计数。
  • 迭代整数N的数字并将freq1[]中每个数字的计数增加1
  • 迭代整数M的数字并将 freq2[]中每个数字的计数增加1
  • 如果freq1[i]freq2[i]都超过0 ,则迭代范围[0, 9]并将计数增加1
  • 最后,完成上述步骤后,将得到的计数打印为所需答案。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count number of digits
// that are common in both N and M
int CommonDigits(int N, int M)
{
    // Stores the count of common digits
    int count = 0;
 
    // Stores the count of digits of N
    int freq1[10] = { 0 };
 
    // Stores the count of digits of M
    int freq2[10] = { 0 };
 
    // Iterate over the digits of N
    while (N > 0) {
 
        // Increment the count of
        // last digit of N
        freq1[N % 10]++;
 
        // Update N
        N = N / 10;
    }
    // Iterate over the digits of M
    while (M > 0) {
 
        // Increment the count of
        // last digit of M
        freq2[M % 10]++;
 
        // Update M
        M = M / 10;
    }
    // Iterate over the range [0, 9]
    for (int i = 0; i < 10; i++) {
 
        // If freq1[i] and freq2[i] both exceeds 0
        if (freq1[i] > 0 & freq2[i] > 0) {
 
            // Increment count by 1
            count++;
        }
    }
 
    // Return the count
    return count;
}
 
// Driver Code
int main()
{
    // Input
    int N = 748294;
    int M = 34298156;
 
    cout << CommonDigits(N, M);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to count number of digits
// that are common in both N and M
static int CommonDigits(int N, int M)
{
     
    // Stores the count of common digits
    int count = 0;
 
    // Stores the count of digits of N
    int freq1[] = new int[10];
 
    // Stores the count of digits of M
    int freq2[] = new int[10];
 
    // Iterate over the digits of N
    while (N > 0)
    {
         
        // Increment the count of
        // last digit of N
        freq1[N % 10]++;
 
        // Update N
        N = N / 10;
    }
     
    // Iterate over the digits of M
    while (M > 0)
    {
         
        // Increment the count of
        // last digit of M
        freq2[M % 10]++;
 
        // Update M
        M = M / 10;
    }
     
    // Iterate over the range [0, 9]
    for(int i = 0; i < 10; i++)
    {
         
        // If freq1[i] and freq2[i] both exceeds 0
        if (freq1[i] > 0 & freq2[i] > 0)
        {
             
            // Increment count by 1
            count++;
        }
    }
 
    // Return the count
    return count;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Input
    int N = 748294;
    int M = 34298156;
 
    System.out.print(CommonDigits(N, M));
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program for the above approach
 
# Function to count number of digits
# that are common in both N and M
def CommonDigits(N, M):
     
    # Stores the count of common digits
    count = 0
 
    # Stores the count of digits of N
    freq1 = [0] * 10
 
    # Stores the count of digits of M
    freq2 = [0] * 10
 
    # Iterate over the digits of N
    while (N > 0):
         
        # Increment the count of
        # last digit of N
        freq1[N % 10] += 1
 
        # Update N
        N = N // 10
         
    # Iterate over the digits of M
    while (M > 0):
         
        # Increment the count of
        # last digit of M
        freq2[M % 10] += 1
 
        # Update M
        M = M // 10
 
    # Iterate over the range [0, 9]
    for i in range(10):
         
        # If freq1[i] and freq2[i] both exceeds 0
        if (freq1[i] > 0 and freq2[i] > 0):
             
            # Increment count by 1
            count += 1
 
    # Return the count
    return count
 
# Driver Code
if __name__ == '__main__':
     
    # Input
    N = 748294
    M = 34298156
 
    print (CommonDigits(N, M))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to count number of digits
// that are common in both N and M
static int CommonDigits(int N, int M)
{
     
    // Stores the count of common digits
    int count = 0;
 
    // Stores the count of digits of N
    int[] freq1 = new int[10];
 
    // Stores the count of digits of M
    int[] freq2 = new int[10];
 
    // Iterate over the digits of N
    while (N > 0)
    {
         
        // Increment the count of
        // last digit of N
        freq1[N % 10]++;
 
        // Update N
        N = N / 10;
    }
     
    // Iterate over the digits of M
    while (M > 0)
    {
         
        // Increment the count of
        // last digit of M
        freq2[M % 10]++;
 
        // Update M
        M = M / 10;
    }
     
    // Iterate over the range [0, 9]
    for(int i = 0; i < 10; i++)
    {
         
        // If freq1[i] and freq2[i]
        // both exceeds 0
        if (freq1[i] > 0 & freq2[i] > 0)
        {
             
            // Increment count by 1
            count++;
        }
    }
 
    // Return the count
    return count;
}
 
// Driver code
static void Main()
{
     
    // Input
    int N = 748294;
    int M = 34298156;
 
    Console.WriteLine(CommonDigits(N, M));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
4

时间复杂度: O(位数(N)+位数(M))
辅助空间: O(10)