Recuento de alfabetos cuyos valores ASCII se pueden formar con los dígitos de N

Dado un número entero N . Puede seleccionar dos dígitos cualquiera de este número (los dígitos pueden ser iguales pero sus posiciones deben ser diferentes) y ordenarlos de cualquiera de las dos formas posibles. Para cada una de estas formas, crea un número de dos dígitos a partir de él (puede contener ceros a la izquierda). Luego, elegirá un carácter correspondiente al valor ASCII igual a este número, es decir, el número 65 corresponde a ‘A’ , 66 a ‘B’ y así sucesivamente. La tarea es contar el número de alfabetos ingleses (minúsculas o mayúsculas) que se pueden elegir de esta manera.
Ejemplos: 
 

Entrada: N = 656 
Salida:
Solo son posibles los caracteres ‘A’ (65) y ‘B’ (66).
Entrada: N = 1623455078 
Salida: 27 
 

Planteamiento: La idea es observar que el total de caracteres posibles son (26 minúsculas + 26 mayúsculas = 52). Entonces, en lugar de generar todas las combinaciones posibles de dos dígitos a partir de N, verifique las ocurrencias de estos 52 caracteres. 
Por lo tanto, cuente las ocurrencias de cada dígito en N y luego, para cada carácter (minúsculas o mayúsculas), encuentre su valor ASCII y verifique si se puede elegir entre los dígitos dados. Imprime el recuento de dichos alfabetos al final.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include<bits/stdc++.h>
 
using namespace std;
 
// Function that returns true if num
// can be formed with the digits in
// digits[] array
bool canBePicked(int digits[], int num)
{
 
    int copyDigits[10];
 
    // Copy of the digits array
    for(int i =0 ; i < 10;i++)
        copyDigits[i]=digits[i];
         
    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] -= 1;
 
        // Remove the last digit
        num = floor(num / 10);
    }
 
    return true;
}
 
// Function to return the count of
// required alphabets
int countAlphabets(long n)
{
 
    int count = 0;
 
    // To store the occurrences of
    // digits (0 - 9)
    int digits[10]= {0};
    while (n > 0)
    {
 
        // Get last digit
        int digit = n % 10;
 
        // Update the occurrence of the digit
        digits[digit] += 1;
 
        // Remove the last digit
        n = floor(n / 10);
    }
 
    // If any lowercase character can be
    // picked from the current digits
    for(int i = 97; i <= 122 ;i ++)
        if (canBePicked(digits, i))
            count += 1;
 
    // If any uppercase character can be
    // picked from the current digits
    for(int i = 65; i < 91;i++)
        if (canBePicked(digits, i))
            count += 1;
 
    // Return the required count
    // of alphabets
    return count;
}
 
// Driver code
int main()
{
 
    long n = 1623455078;
    cout<<(countAlphabets(n));
}
 
// This code is contributed by chitranayal

Java

// 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));
    }
}

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#

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

PHP

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

Javascript

<script>
 //Javascript implementation of the approach
 
// Function that returns true if num
// can be formed with the digits in
// digits[] array
function canBePicked( digits, num)
{
 
    var copyDigits = new Array(10);;
 
    // Copy of the digits array
    for(var i =0 ; i < 10;i++)
        copyDigits[i]=digits[i];
         
    while (num > 0)
    {
        // Get last digit
        var 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
function countAlphabets( n)
{
 
    var count = 0;
 
    // To store the occurrences of
    // digits (0 - 9)
    var digits = new Array(10);
    digits.fill(0);
     
    while (n > 0)
    {
 
        // Get last digit
        var 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(var i = 97; i <= 122 ;i ++)
        if (canBePicked(digits, i))
            count += 1;
 
    // If any uppercase character can be
    // picked from the current digits
    for(var i = 65; i < 91;i++)
        if (canBePicked(digits, i))
            count += 1;
 
    // Return the required count
    // of alphabets
    return count;
}
 
var n = 1623455078;
document.write(countAlphabets(n));
 
// This code is contributed by SoumikMondal
</script>
Producción: 

27

 

Espacio Auxiliar: O(10)

Publicación traducida automáticamente

Artículo escrito por ayushgoyal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *