📜  两个给定数字中存在的公用位数

📅  最后修改于: 2021-04-17 16:24: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


输出:
4

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