Compruebe si la frecuencia de cada dígito es menor que el dígito

Dado un entero n, la tarea es verificar si la frecuencia de cada dígito del número es menor o igual que el dígito mismo. 
Ejemplos: 
 

Input : 51241
Output : False

Input : 1425243
Output : True

Enfoque ingenuo: Comience desde 0 y cuente la frecuencia para cada dígito hasta 9, si en algún lugar la frecuencia es mayor que el valor del dígito, devuelva falso, de lo contrario, devuelva verdadero.
 

C++

// A C++ program to validate a number
#include<bits/stdc++.h>
using namespace std;
 
// Function to validate number (Check if
// frequency of a digit is less than the
// digit itself or not)
bool validate(long long int n)
{
    for (int i=0; i<10; i++)
    {
        long long int temp = n;
        int count = 0;
        while (temp)
        {
            // If current digit of temp is
            // same as i
            if (temp % 10 == i)
                count++;
 
            // if frequency is greater than
            // digit value, return false
            if (count > i)
                return false;
 
            temp /= 10;
        }
    }
    return true;
}
 
// driver program
int main()
{
    long long int n = 1552793;
    if (validate(n))
        cout << "True";
    else
        cout << "False";
    return 0;
}

Java

// Java program to validate a number
import java .io.*;
 
public class GFG {
 
// Function to validate number (Check if
// frequency of a digit is less than the
// digit itself or not)
static boolean validate(long n)
{
    for (int i = 0; i < 10; i++)
    {
        long temp = n;
        int count = 0;
        while (temp > 0)
        {
            // If current digit of
            // temp is same as i
            if (temp % 10 == i)
                count++;
 
            // if frequency is greater than
            // digit value, return false
            if (count > i)
                return false;
 
            temp /= 10;
        }
    }
    return true;
}
 
    // Driver Code
    static public void main (String[] args)
    {
            long n = 1552793;
        if (validate(n))
            System.out.println("True");
        else
            System.out.println("False");
    }
}
 
// This code is contributed by vt_m.

Python3

# Python3 program to validate a number
 
# Function to validate number (Check if
# frequency of a digit is less than the
# digit itself or not)
def validate(n):
 
    for i in range(10):
        temp = n;
        count = 0;
        while (temp):
             
            # If current digit of temp is
            # same as i
            if (temp % 10 == i):
                count+=1;
 
            # if frequency is greater than
            # digit value, return false
            if (count > i):
                return -1;
 
            temp //= 10;
 
    return 1;
 
# Driver Code
n = 1552793;
geek = "True" if validate(n) else "False";
print(geek);
 
# This code is contributed by mits

C#

// C# program to validate a number
using System;
 
public class GFG {
 
// Function to validate number (Check if
// frequency of a digit is less than the
// digit itself or not)
static bool validate(long n)
{
    for (int i = 0; i < 10; i++)
    {
        long temp = n;
        int count = 0;
        while (temp > 0)
        {
            // If current digit of
            // temp is same as i
            if (temp % 10 == i)
                count++;
 
            // if frequency is greater than
            // digit value, return false
            if (count > i)
                return false;
 
            temp /= 10;
        }
    }
    return true;
}
 
    // Driver Code
    static public void Main(String[] args)
    {
            long n = 1552793;
        if (validate(n))
            Console.WriteLine("True");
        else
            Console.WriteLine("False");
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP program to validate a number
 
// Function to validate number
// (Check if frequency of a
// digit is less than the
// digit itself or not)
function validate($n)
{
    for ($i = 0; $i < 10; $i++)
    {
        $temp = $n;
        $count = 0;
        while ($temp)
        {
             
            // If current digit
            // of temp is same
            // as i
            if ($temp % 10 == $i)
                $count++;
 
            // if frequency is
            // greater than digit
            // value, return false
            if ($count > $i)
                return -1;
 
            $temp /= 10;
        }
    }
    return 1;
}
 
    // Driver Code
    $n = 1552793;
    $geek = validate($n) ?"True" :"False";
    echo($geek);
 
// This code is contributed by Ajit
?>

Javascript

<script>
 
// JavaScript program for the above approach
 
// Function to validate number (Check if
// frequency of a digit is less than the
// digit itself or not)
function  validate(n)
{
    for (let i = 0; i < 10; i++)
    {
        let temp = n;
        let count = 0;
        while (temp > 0)
        {
            // If current digit of
            // temp is same as i
            if (temp % 10 == i)
                count++;
 
            // if frequency is greater than
            // digit value, return false
            if (count > i)
                return false;
 
            temp /= 10;
        }
    }
    return true;
}
 
// Driver Code
     
    let n = 1552793;
    if (validate(n))
       document.write("True");
    else
      document.write("False");
 
// This code is contributed by susmitakundugoaldanga.
</script>

Producción: 
 

True

Complejidad de tiempo: O(10 * log 10 n), donde n representa el entero dado.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.

Enfoque eficiente: es almacenar la frecuencia de cada dígito y si en algún lugar la frecuencia es mayor que el valor del dígito, devolver falso, de lo contrario devolver verdadero.
 

C++

// A C++ program to validate a number
#include<bits/stdc++.h>
using namespace std;
 
// Function to validate number (Check if
// frequency of a digit is less than the
// digit itself or not)
bool validate(long long int n)
{
    int count[10] = {0}; 
    while (n)
    {
        // calculate frequency of each digit
        int r = n % 10;
 
        // If count is already r, then
        // incrementing it would invalidate,
        // hence we return false.
        if (count[r] == r)
           return false; 
 
        count[r]++;
        n /= 10;
    }
 
    return true;
}
 
// driver program
int main()
{
    long long int n = 1552793;
    if (validate(n))
        cout << "True";
    else
        cout << "False";
    return 0;
}

Java

// A Java program to
// validate a number
import java.io.*;
 
class GFG
{
     
// Function to validate
// number (Check if frequency
// of a digit is less than
// the digit itself or not)
static boolean validate(long n)
{
    int count[] = new int[10] ;
    while (n > 0)
    {
        // calculate frequency
        // of each digit
        int r = (int)n % 10;
 
        // If count is already r,
        // then incrementing it
        // would invalidate,
        // hence we return false.
        if (count[r] == r)
        return false;
 
        count[r]++;
        n /= 10;
    }
 
    return true;
}
 
// Driver Code
public static void main (String[] args)
{
    long n = 1552793;
    if (validate(n))
        System.out.println("True");
    else
        System.out.println("False");
}
}
 
// This code is contributed by ajit

Python3

# A Python3 program to validate a number
import math as mt
 
# Function to validate number (Check if
# frequency of a digit is less than the
# digit itself or not)
def validate(n):
 
    count = [0 for i in range(10)]
    while (n > 0):
     
        # calculate frequency of each digit
        r = n % 10
 
        # If count is already r, then
        # incrementing it would invalidate,
        # hence we return false.
        if (count[r] == r):
            return False
 
        count[r] += 1
        n = n // 10
     
    return True
 
# Driver Code
n = 1552793
if (validate(n)):
    print("True")
else:
    print("False")
 
# This code is contributed by
# Mohit kumar 29

C#

// A C# program to validate a number
using System;
 
class GFG
{
// Function to validate number
// (Check if frequency of a digit is
// less than the digit itself or not)
static bool validate(long n)
{
    int []count = new int[10] ;
    while (n > 0)
    {
        // calculate frequency
        // of each digit
        int r = (int)n % 10;
 
        // If count is already r, then 
        // incrementing it would invalidate,
        // hence we return false.
        if (count[r] == r)
        return false;
 
        count[r]++;
        n /= 10;
    }
 
    return true;
}
 
// Driver Code
static public void Main ()
{
    long n = 1552793;
    if (validate(n))
        Console.WriteLine("True");
    else
        Console.WriteLine("False");
}
}
 
// This code is contributed by jit_t

PHP

<?php
// A PHP program to validate a number
 
// Function to validate number (Check if
// frequency of a digit is less than the
// digit itself or not)
function validate($n)
{
    $count=array(10);
    while ($n)
    {
        // calculate frequency of each digit
        $r = $n % 10;
 
        // If count is already r, then
        // incrementing it would invalidate,
        // hence we return false.
        if (($count[$r] == $r))
        {
            return false;
        }
 
        $count[$r] = $count[$r] + 1;
        $n = $n / 10;
    }
 
    return true;
}
 
// Driver Code
    $n = 1552793;
    $geek = validate($n) ?"True" :"False";
    echo($geek);
?>

Javascript

<script>
 
// A JavaScript program to validate a number
 
// Function to validate number (Check if
// frequency of a digit is less than the
// digit itself or not)
function validate(n)
{
    let count = new Uint8Array(10);
    while (n)
    {
        // calculate frequency of each digit
        let r = n % 10;
 
        // If count is already r, then
        // incrementing it would invalidate,
        // hence we return false.
        if (count[r] == r)
        return false;
 
        count[r]++;
        n = Math.floor(n / 10);
    }
 
    return true;
}
 
// driver program
 
    let n = 1552793;
    if (validate(n))
        document.write( "True");
    else
        document.write("False");
 
 
// This code is contributed by Surbhi Tyagi.
 
</script>

Producción: 

True

Complejidad de tiempo: O(log10n), donde n representa el entero dado.
Espacio auxiliar: O(10), no se requiere espacio adicional, por lo que es una constante.

Este artículo es una contribución de Shivam Pradhan (anuj_charm) . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 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 *