Count of alphabets whose ASCII values can be formed with the digits of N
Last Updated :
08 Feb, 2024
Given an integer N. You can select any two digits from this number (the digits can be the 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 is (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:
C++
#include<bits/stdc++.h>
using namespace std;
bool canBePicked( int digits[], int num)
{
int copyDigits[10];
for ( int i =0 ; i < 10;i++)
copyDigits[i]=digits[i];
while (num > 0)
{
int digit = num % 10;
if (copyDigits[digit] == 0)
return false ;
else
copyDigits[digit] -= 1;
num = floor (num / 10);
}
return true ;
}
int countAlphabets( long n)
{
int count = 0;
int digits[10]= {0};
while (n > 0)
{
int digit = n % 10;
digits[digit] += 1;
n = floor (n / 10);
}
for ( int i = 97; i <= 122 ;i ++)
if (canBePicked(digits, i))
count += 1;
for ( int i = 65; i < 91;i++)
if (canBePicked(digits, i))
count += 1;
return count;
}
int main()
{
long n = 1623455078;
cout<<(countAlphabets(n));
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static boolean canBePicked( int digits[], int num)
{
int copyDigits[] = digits.clone();
while (num > 0 ) {
int digit = num % 10 ;
if (copyDigits[digit] == 0 )
return false ;
else
copyDigits[digit]--;
num /= 10 ;
}
return true ;
}
static int countAlphabets( int n)
{
int i, count = 0 ;
int digits[] = new int [ 10 ];
while (n > 0 ) {
int digit = n % 10 ;
digits[digit]++;
n /= 10 ;
}
for (i = 'a' ; i <= 'z' ; i++)
if (canBePicked(digits, i))
count++;
for (i = 'A' ; i <= 'Z' ; i++)
if (canBePicked(digits, i))
count++;
return count;
}
public static void main(String[] args)
{
int n = 1623455078 ;
System.out.println(countAlphabets(n));
}
}
|
Python3
import math
def canBePicked(digits, num):
copyDigits = [];
for i in range ( len (digits)):
copyDigits.append(digits[i]);
while (num > 0 ):
digit = num % 10 ;
if (copyDigits[digit] = = 0 ):
return False ;
else :
copyDigits[digit] - = 1 ;
num = math.floor(num / 10 );
return True ;
def countAlphabets(n):
count = 0 ;
digits = [ 0 ] * 10 ;
while (n > 0 ):
digit = n % 10 ;
digits[digit] + = 1 ;
n = math.floor(n / 10 );
for i in range ( ord ( 'a' ), ord ( 'z' ) + 1 ):
if (canBePicked(digits, i)):
count + = 1 ;
for i in range ( ord ( 'A' ), ord ( 'Z' ) + 1 ):
if (canBePicked(digits, i)):
count + = 1 ;
return count;
n = 1623455078 ;
print (countAlphabets(n));
|
C#
using System;
class GFG
{
static bool canBePicked( int []digits, int num)
{
int []copyDigits = ( int []) digits.Clone();
while (num > 0)
{
int digit = num % 10;
if (copyDigits[digit] == 0)
return false ;
else
copyDigits[digit]--;
num /= 10;
}
return true ;
}
static int countAlphabets( int n)
{
int i, count = 0;
int [] digits = new int [10];
while (n > 0)
{
int digit = n % 10;
digits[digit]++;
n /= 10;
}
for (i = 'a' ; i <= 'z' ; i++)
if (canBePicked(digits, i))
count++;
for (i = 'A' ; i <= 'Z' ; i++)
if (canBePicked(digits, i))
count++;
return count;
}
public static void Main()
{
int n = 1623455078;
Console.WriteLine(countAlphabets(n));
}
}
|
Javascript
<script>
function canBePicked( digits, num)
{
var copyDigits = new Array(10);;
for ( var i =0 ; i < 10;i++)
copyDigits[i]=digits[i];
while (num > 0)
{
var digit = num % 10;
if (copyDigits[digit] == 0)
return false ;
else
copyDigits[digit] -= 1;
num = Math.floor(num / 10);
}
return true ;
}
function countAlphabets( n)
{
var count = 0;
var digits = new Array(10);
digits.fill(0);
while (n > 0)
{
var digit = n % 10;
digits[digit] += 1;
n = Math.floor(n / 10);
}
for ( var i = 97; i <= 122 ;i ++)
if (canBePicked(digits, i))
count += 1;
for ( var i = 65; i < 91;i++)
if (canBePicked(digits, i))
count += 1;
return count;
}
var n = 1623455078;
document.write(countAlphabets(n));
</script>
|
PHP
<?php
function canBePicked( $digits , $num )
{
$copyDigits = array ();
for ( $i = 0; $i < sizeof( $digits ); $i ++)
$copyDigits [ $i ] = $digits [ $i ];
while ( $num > 0)
{
$digit = $num % 10;
if ( $copyDigits [ $digit ] == 0)
return false;
else
$copyDigits [ $digit ]--;
$num = floor ( $num / 10);
}
return true;
}
function countAlphabets( $n )
{
$count = 0;
$digits = array_fill (0, 10, 0);
while ( $n > 0)
{
$digit = $n % 10;
$digits [ $digit ]++;
$n = floor ( $n / 10);
}
for ( $i = ord( 'a' ); $i <= ord( 'z' ); $i ++)
if (canBePicked( $digits , $i ))
$count ++;
for ( $i = ord( 'A' );
$i <= ord( 'Z' ); $i ++)
if (canBePicked( $digits , $i ))
$count ++;
return $count ;
}
$n = 1623455078;
echo countAlphabets( $n );
?>
|
Time Complexity: O(n log n)
Auxiliary Space: O(10)
Share your thoughts in the comments
Please Login to comment...