📜  检查数字是否为 Pangram

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

给定一个整数N ,任务是检查给定的数字是否是全字母组合。
注意:一个 Pangram 号码至少包含每个数字[ 0-9 ]一次。

例子:

基于集合的方法:想法是使用集合来存储N 中存在的不同数字的计数。请按照以下步骤解决问题:

  • 将数字 N 转换为等效的字符串。
  • 将字符串转换为集合。
  • 如果 Set 的大小为10 ,则它包含所有不同的可能数字[0 – 9] 。因此,打印“是”
  • 否则,打印“否”

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to check if N
// is a Pangram or not
string numberPangram(string N)
{
 
    // Add all characters pf arrNum to set
    set setNum;
 
    for (int i = 0; i < N.length(); i++) {
        setNum.insert(N[i]);
    }
 
    // If the length of set is 10
    // The number is a Pangram
    if (setNum.size() == 10)
        return "True";
    else
        return "False";
}
 
// Driver code
int main()
{
    string N = "10239876540022";
    cout << (numberPangram(N));
}
 
// This code is contributed by ukasp.


Java
// Java implementation of above approach
import java.math.BigInteger;
import java.util.HashSet;
 
class GFG{
     
// Function to check if N
// is a Pangram or not
static String numberPangram(BigInteger N)
{
     
    // Stores equivalent string
    // representation of N
    String num = N.toString();
 
    // Convert the string to Character array
    char[] arrNum = num.toCharArray();
 
    // Add all characters pf arrNum to set
    HashSet setNum = new HashSet();
 
    for(char ch : arrNum)
    {
        setNum.add(ch);
    }
     
    // If the length of set is 10
    // The number is a Pangram
    if (setNum.size() == 10)
        return "True";
    else
        return "False";
}
 
// Driver code
public static void main(String[] args)
{
    BigInteger N = new BigInteger("10239876540022");
    System.out.print(numberPangram(N));
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 implementation of above approach
 
# Function to check if N
# is a Pangram or not
def numberPangram(N):
   
    # Stores equivalent string
    # representation of N
    num = str(N)
     
    # Convert the string to set
    setnum = set(num)
     
    # If the length of set is 10
    if(len(setnum) == 10):
       
          # The number is a Pangram
        return True
    else:
        return False
 
 
# Driver Code
 
N = 10239876540022
print(numberPangram(N))


C#
// C# implementation of above approach
using System;
using System.Globalization;
using System.Numerics;
using System.Collections.Generic;
class GFG
{
     
// Function to check if N
// is a Pangram or not
static String numberPangram(ulong  N)
{
     
    // Stores equivalent string
    // representation of N
    string num = N.ToString();
 
    // Convert the string to Character array
    char[] arrNum = num.ToCharArray();
 
    // Add all characters pf arrNum to set
   HashSet setNum = new HashSet();
 
    foreach(char ch in arrNum)
    {
        setNum.Add(ch);
    }
     
    // If the length of set is 10
    // The number is a Pangram
    if (setNum.Count == 10)
        return "True";
    else
        return "False";
}
 
// Driver Code
    static void Main() {
      ulong  N = 10239876540022;
      Console.Write(numberPangram(N));
      }
}
 
// This code is contributed by SoumikMondal


Javascript


Python3
# Python implementation of above approach
 
from collections import Counter
 
# Function to check if
# N is a Pangram or not
def numberPangram(N):
   
    # Stores equivalent string
    # representation of N
    num = str(N)
     
    # Count frequencies of
    # digits present in N
    frequency = Counter(num)
     
    # If the length of the
    # dictionary frequency is 10
    if(len(frequency) == 10):
       
          # The number is a Pangram
        return True
    else:
        return False
 
 
# Driver Code
 
N =10239876540022
print(numberPangram(N))


输出:
True

时间复杂度: O(log 10 N * log(log 10 N))
辅助空间: O(1)

基于哈希的方法:按照以下步骤解决问题:

  • 将 N 转换为其等效的字符串 。
  • 计算此字符串中所有字符的频率。 Counter()函数可在Python用于此目的。
  • 如果存储频率的 Dictionary / HashMap 的长度为10 ,则该数字包含所有可能的不同数字。因此,打印“是”。
  • 否则,打印“否”

下面是上述方法的实现:

蟒蛇3

# Python implementation of above approach
 
from collections import Counter
 
# Function to check if
# N is a Pangram or not
def numberPangram(N):
   
    # Stores equivalent string
    # representation of N
    num = str(N)
     
    # Count frequencies of
    # digits present in N
    frequency = Counter(num)
     
    # If the length of the
    # dictionary frequency is 10
    if(len(frequency) == 10):
       
          # The number is a Pangram
        return True
    else:
        return False
 
 
# Driver Code
 
N =10239876540022
print(numberPangram(N))
输出:
True

时间复杂度: O(log 10 N * log(log 10 N))
辅助空间: O(1)