📜  检查数字是否为Pangram

📅  最后修改于: 2021-04-17 16:44:20             🧑  作者: Mango

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

例子:

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

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

下面是上述方法的实现:

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))


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 ,则该数字包含所有可能的不同数字。因此,打印“是”。
  • 否则,打印“否”

下面是上述方法的实现:

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)