Count of alphabets whose ASCII values can be formed with the digits of N

Given an integer N. You can select any two digits from this number (the digits can be same but their positions should be different) and order them in any one of the two possible ways. For each of these ways, you create a two digit number from it (might contain leading zeros). Then, you will pick a character corresponding to the ASCII value equal to this number, i.e. the number 65 corresponds to ‘A’, 66 to ‘B’ and so on. The task is to count the number of english alphabets (lowercase or uppercase) that can be picked in this way.

Examples:

Input: N = 656
Output: 2
Only the characters ‘A’ (65) and ‘B’ (66) are possible.

Input: N = 1623455078
Output: 27



Approach: The idea is to observe that the total number of possible characters are (26 lowercase + 26 uppercase = 52). So, instead of generating all possible combinations of two digits from N, check the occurrences of these 52 characters.

Therefore, count the occurrences of each digit in N then for every character (lowercase or uppercase), find its ASCII value and check whether it can be picked from the given digits. Print the count of such alphabets in the end.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG {
  
    // Function that returns true if num can be formed
    // with the digits in digits[] array
    static boolean canBePicked(int digits[], int num)
    {
        // Copy of the digits array
        int copyDigits[] = digits.clone();
        while (num > 0) {
  
            // Get last digit
            int digit = num % 10;
  
            // If digit array doesn't contain current digit
            if (copyDigits[digit] == 0)
                return false;
  
            // One occurrence is used
            else
                copyDigits[digit]--;
  
            // Remove the last digit
            num /= 10;
        }
  
        return true;
    }
  
    // Function to return the count of required alphabets
    static int countAlphabets(int n)
    {
        int i, count = 0;
  
        // To store the occurrences of digits (0 - 9)
        int digits[] = new int[10];
        while (n > 0) {
  
            // Get last digit
            int digit = n % 10;
  
            // Update the occurrence of the digit
            digits[digit]++;
  
            // Remove the last digit
            n /= 10;
        }
  
        // If any lowercase character can be picked 
        // from the current digits
        for (i = 'a'; i <= 'z'; i++)
            if (canBePicked(digits, i))
                count++;
  
        // If any uppercase character can be picked 
        // from the current digits
        for (i = 'A'; i <= 'Z'; i++)
            if (canBePicked(digits, i))
                count++;
  
        // Return the required count of alphabets
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 1623455078;
        System.out.println(countAlphabets(n));
    }
}

chevron_right


Python3

# Python3 implementation of the approach
import math

# Function that returns true if num
# can be formed with the digits in
# digits[] array
def canBePicked(digits, num):

copyDigits = [];

# Copy of the digits array
for i in range(len(digits)):
copyDigits.append(digits[i]);

while (num > 0):

# Get last digit
digit = num % 10;

# If digit array doesn’t contain
# current digit
if (copyDigits[digit] == 0):
return False;

# One occurrence is used
else:
copyDigits[digit] -= 1;

# Remove the last digit
num = math.floor(num / 10);

return True;

# Function to return the count of
# required alphabets
def countAlphabets(n):

count = 0;

# To store the occurrences of
# digits (0 – 9)
digits = [0] * 10;
while (n > 0):

# Get last digit
digit = n % 10;

# Update the occurrence of the digit
digits[digit] += 1;

# Remove the last digit
n = math.floor(n / 10);

# If any lowercase character can be
# picked from the current digits
for i in range(ord(‘a’), ord(‘z’) + 1):
if (canBePicked(digits, i)):
count += 1;

# If any uppercase character can be
# picked from the current digits
for i in range(ord(‘A’), ord(‘Z’) + 1):
if (canBePicked(digits, i)):
count += 1;

# Return the required count
# of alphabets
return count;

# Driver code
n = 1623455078;
print(countAlphabets(n));

# This code is contributed by mits

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG 
{
  
    // Function that returns true
    // if num can be formed with
    // the digits in digits[] array
    static bool canBePicked(int []digits, int num)
    {
        // Copy of the digits array
        int []copyDigits = (int[]) digits.Clone();
        while (num > 0)
        {
  
            // Get last digit
            int digit = num % 10;
  
            // If digit array doesn't 
            // contain current digit
            if (copyDigits[digit] == 0)
                return false;
  
            // One occurrence is used
            else
                copyDigits[digit]--;
  
            // Remove the last digit
            num /= 10;
        }
  
        return true;
    }
  
    // Function to return the count 
    // of required alphabets
    static int countAlphabets(int n)
    {
        int i, count = 0;
  
        // To store the occurrences 
        // of digits (0 - 9)
        int[] digits = new int[10];
        while (n > 0)
        {
  
            // Get last digit
            int digit = n % 10;
  
            // Update the occurrence of the digit
            digits[digit]++;
  
            // Remove the last digit
            n /= 10;
        }
  
        // If any lowercase character can be  
        // picked from the current digits
        for (i = 'a'; i <= 'z'; i++)
            if (canBePicked(digits, i))
                count++;
  
        // If any uppercase character can be  
        // picked from the current digits
        for (i = 'A'; i <= 'Z'; i++)
            if (canBePicked(digits, i))
                count++;
  
        // Return the required count of alphabets
        return count;
    }
  
    // Driver code
    public static void Main()
    {
        int n = 1623455078;
        Console.WriteLine(countAlphabets(n));
    }
}
  
// This code is contributed by 
// tufan_gupta2000

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach 
  
// Function that returns true if num 
// can be formed with the digits in
// digits[] array 
function canBePicked($digits, $num
    $copyDigits = array();
      
    // Copy of the digits array 
    for($i = 0; $i < sizeof($digits); $i++)
        $copyDigits[$i] = $digits[$i]; 
          
    while ($num > 0) 
    
  
        // Get last digit 
        $digit = $num % 10; 
  
        // If digit array doesn't contain
        // current digit 
        if ($copyDigits[$digit] == 0) 
            return false; 
  
        // One occurrence is used 
        else
            $copyDigits[$digit]--; 
  
        // Remove the last digit 
        $num = floor($num / 10); 
    
  
    return true; 
  
// Function to return the count of
// required alphabets 
function countAlphabets($n
    $count = 0; 
  
    // To store the occurrences of 
    // digits (0 - 9) 
    $digits = array_fill(0, 10, 0); 
    while ($n > 0) 
    
  
        // Get last digit 
        $digit = $n % 10; 
  
        // Update the occurrence of the digit 
        $digits[$digit]++; 
  
        // Remove the last digit 
        $n = floor($n / 10); 
    
  
    // If any lowercase character can be 
    // picked from the current digits 
    for ($i = ord('a'); $i <= ord('z'); $i++) 
        if (canBePicked($digits, $i)) 
            $count++; 
  
    // If any uppercase character can be  
    // picked from the current digits 
    for ($i = ord('A'); 
         $i <= ord('Z'); $i++) 
        if (canBePicked($digits, $i)) 
            $count++; 
  
    // Return the required count 
    // of alphabets 
    return $count
  
// Driver code 
$n = 1623455078; 
echo countAlphabets($n); 
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

27


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.