Check if a number is a Pangram or not
Last Updated :
31 Jan, 2023
Given an integer N, the task is to check whether the given number is a pangram or not.
Note: A Pangram Number contains every digit [0- 9] at least once.
Examples:
Input : N = 10239876540022
Output : Yes
Explanation: N contains all the digits from 0 to 9. Therefore, it is a pangram.
Input : N = 234567890
Output : No
Explanation: N doesn’t contain the digit 1. Therefore, it is not a pangram.
Set-based Approach: The idea is to use Sets to store the count of distinct digits present in N. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string numberPangram(string N)
{
set< char > setNum;
for ( int i = 0; i < N.length(); i++) {
setNum.insert(N[i]);
}
if (setNum.size() == 10)
return "True" ;
else
return "False" ;
}
int main()
{
string N = "10239876540022" ;
cout << (numberPangram(N));
}
|
Java
import java.math.BigInteger;
import java.util.HashSet;
class GFG{
static String numberPangram(BigInteger N)
{
String num = N.toString();
char [] arrNum = num.toCharArray();
HashSet<Character> setNum = new HashSet<Character>();
for ( char ch : arrNum)
{
setNum.add(ch);
}
if (setNum.size() == 10 )
return "True" ;
else
return "False" ;
}
public static void main(String[] args)
{
BigInteger N = new BigInteger( "10239876540022" );
System.out.print(numberPangram(N));
}
}
|
Python3
def numberPangram(N):
num = str (N)
setnum = set (num)
if ( len (setnum) = = 10 ):
return True
else :
return False
N = 10239876540022
print (numberPangram(N))
|
C#
using System;
using System.Globalization;
using System.Numerics;
using System.Collections.Generic;
class GFG
{
static String numberPangram( ulong N)
{
string num = N.ToString();
char [] arrNum = num.ToCharArray();
HashSet< char > setNum = new HashSet< char >();
foreach ( char ch in arrNum)
{
setNum.Add(ch);
}
if (setNum.Count == 10)
return "True" ;
else
return "False" ;
}
static void Main() {
ulong N = 10239876540022;
Console.Write(numberPangram(N));
}
}
|
Javascript
<script>
function numberPangram(N)
{
var setNum = new Set();
for ( var i = 0; i < N.length; i++) {
setNum.add(N[i]);
}
if (setNum.size == 10)
return "True" ;
else
return "False" ;
}
var N = "10239876540022" ;
document.write(numberPangram(N));
</script>
|
Time Complexity: O(log10N * log(log10N))
Auxiliary Space: O(1)
Hashing-based Approach: Follow the steps to solve the problem:
Below is the implementation of the above approach:
C++
#include <iostream>
#include <string>
#include <map>
bool numberPangram( long N) {
std::string num = std::to_string(N);
std::map< char , int > frequency;
for ( int i = 0; i < num.length(); i++) {
char digit = num[i];
frequency[digit]++;
}
if (frequency.size() == 10) {
return true ;
} else {
return false ;
}
}
int main() {
long N = 10239876540022;
std::cout << (numberPangram(N) ? "true" : "false" ) << std::endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args) {
long N = 10239876540022L;
boolean result = numberPangram(N);
System.out.println(result);
}
public static boolean numberPangram( long N) {
String num = Long.toString(N);
Map<Character, Integer> frequency = new HashMap<>();
for ( int i = 0 ; i < num.length(); i++) {
char digit = num.charAt(i);
if (frequency.containsKey(digit)) {
frequency.put(digit, frequency.get(digit) + 1 );
} else {
frequency.put(digit, 1 );
}
}
if (frequency.size() == 10 )
{
return true ;
} else {
return false ;
}
}
}
|
Python3
from collections import Counter
def numberPangram(N):
num = str (N)
frequency = Counter(num)
if ( len (frequency) = = 10 ):
return True
else :
return False
N = 10239876540022
print (numberPangram(N))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void Main( string [] args)
{
long N = 10239876540022L;
bool result = NumberPangram(N);
Console.WriteLine(result);
}
static bool NumberPangram( long N)
{
string num = N.ToString();
Dictionary< char , int > frequency
= new Dictionary< char , int >();
for ( int i = 0; i < num.Length; i++) {
char digit = num[i];
if (frequency.ContainsKey(digit)) {
frequency[digit]++;
}
else {
frequency.Add(digit, 1);
}
}
if (frequency.Count == 10)
{
return true ;
}
else {
return false ;
}
}
}
|
Javascript
function numberPangram(N) {
let num = N.toString();
let frequency = {};
for (let i = 0; i < num.length; i++) {
let digit = num[i];
if (frequency[digit]) {
frequency[digit] += 1;
} else {
frequency[digit] = 1;
}
}
if (Object.keys(frequency).length === 10) {
return True;
} else {
return False;
}
}
let N = 10239876540022;
console.log(numberPangram(N));
|
Time Complexity: O(log10N * log(log10N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...