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:
- Convert the number N to equivalent string.
- Convert the string to a set.
- If the size of the Set is 10, then it contains all the distinct possible digits [0 – 9]. Therefore, print “Yes”.
- Otherwise, print “No”
Below is the implementation of the above approach:
C++
// C++ implementation of above approach #include <bits/stdc++.h> 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< char > 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<Character> setNum = new HashSet<Character>(); 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< char > setNum = new HashSet< char >(); 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
<script> // Javascript implementation of above approach // Function to check if N // is a Pangram or not function numberPangram(N) { // Add all characters pf arrNum to set var setNum = new Set(); for ( var i = 0; i < N.length; i++) { setNum.add(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 var N = "10239876540022" ; document.write(numberPangram(N)); </script> |
True
Time Complexity: O(log10N * log(log10N))
Auxiliary Space: O(1)
Hashing-based Approach: Follow the steps to solve the problem:
- Convert N to its equivalent string.
- Calculate frequencies of all characters in this string. Counter() function can be used for this purpose in Python.
- If the length of the Dictionary / HashMap storing the frequencies is 10, the number contains all possible distinct digits. Therefore, print “Yes“.
- Otherwise, print “No”.
Below is the implementation of the above approach:
C++
// C++ code to implement the approach #include <iostream> #include <string> #include <map> bool numberPangram( long N) { // Stores equivalent string representation of N std::string num = std::to_string(N); // Count frequencies of digits present in N std::map< char , int > frequency; for ( int i = 0; i < num.length(); i++) { char digit = num[i]; frequency[digit]++; } // If the size of the map frequency is 10 if (frequency.size() == 10) { // The number is a Pangram return true ; } else { return false ; } } // Driver code int main() { long N = 10239876540022; std::cout << (numberPangram(N) ? "true" : "false" ) << std::endl; return 0; } // This code is contributed by phasing17 |
Java
import java.util.*; class GFG { // Driver code public static void main(String[] args) { long N = 10239876540022L; boolean result = numberPangram(N); System.out.println(result); } public static boolean numberPangram( long N) { // Stores equivalent string representation of N String num = Long.toString(N); // Count frequencies of digits present in 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 the length of the dictionary frequency is 10 if (frequency.size() == 10 ) { // The number is a Pangram return true ; } else { return false ; } } } // This code is contributed by phasing17. |
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)) |
C#
// C# code to implement the approach using System; using System.Collections.Generic; class GFG { // Driver Code static void Main( string [] args) { long N = 10239876540022L; // Function call bool result = NumberPangram(N); Console.WriteLine(result); } static bool NumberPangram( long N) { // Stores equivalent string representation of N string num = N.ToString(); // Count frequencies of digits present in N 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 the length of the dictionary frequency is 10 if (frequency.Count == 10) { // The number is a Pangram return true ; } else { return false ; } } } // This code is contributed by phasing17. |
Javascript
// JavaScript implementation of above approach // Function to check if // N is a Pangram or not function numberPangram(N) { // Stores equivalent string // representation of N let num = N.toString(); // Count frequencies of // digits present in N 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 the length of the // dictionary frequency is 10 if (Object.keys(frequency).length === 10) { // The number is a Pangram return True; } else { return False; } } // Driver Code let N = 10239876540022; console.log(numberPangram(N)); |
True
Time Complexity: O(log10N * log(log10N))
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!