Dado un número N , la tarea es verificar si la frecuencia de cada dígito en un número es igual a su valor o no
. Ejemplos:
Entrada : N = 3331
Salida : Sí
Explicación : Es un número válido ya que la frecuencia de 3 es 3 y la frecuencia de 1 es 1
Entrada : N = 121
Salida : No
Explicación : No es un número válido ya que la frecuencia de 1 es 2 , y la frecuencia de 2 es 1
Enfoque : la tarea se puede resolver almacenando las frecuencias de un dígito en un mapa hash y luego iterando el mapa hash para verificar si la frecuencia del dígito es igual a su valor o no.
A continuación se muestra la implementación del enfoque anterior:
C++14
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if the number // is valid or not void check(int N) { // Stores the frequencies // of digits unordered_map<char, int> occ; while (N > 0) { // Extracting the digits int r = N % 10; // Incrementing the frequency occ[r]++; N /= 10; } // Iterating the hashmap for (auto i : occ) { // Check if frequency of digit // is equal to its value or not if (i.first != i.second) { cout << "No\n"; return; } } cout << "Yes\n"; } int main() { int N = 3331; check(N); return 0; }
Java
// Java program for the above approach import java.io.*; import java.util.HashMap; import java.util.Map; class GFG { // Function to check if the number // is valid or not public static void check(Integer N) { // Stores the frequencies // of digits HashMap<Integer, Integer> occ = new HashMap<>(); while (N > 0) { // Extracting the digits Integer r = N % 10; if (occ.containsKey(r)) { // If char is present in charCountMap, // incrementing it's count by 1 occ.put(r, occ.get(r) + 1); } else { // If char is not present in charCountMap, // putting this char to charCountMap with 1 // as it's value occ.put(r, 1); } N = N / 10; } for (Map.Entry<Integer, Integer> e : occ.entrySet()) { if (e.getKey() != e.getValue()) { System.out.print("NO"); return; } } System.out.print("Yes"); } // Driver code public static void main(String[] args) { Integer N = 3331; check(N); } } // This code is contributed by Potta Lokesh
Python3
# Python program for the above approach # Function to check if the number # is valid or not def check(N): # Stores the frequencies # of digits occ = dict(); while (N > 0): # Extracting the digits r = N % 10; # Incrementing the frequency if r in occ: occ[r] += 1 else: occ[r] = 1 N = N // 10 # Iterating the hashmap for i in occ.keys(): # Check if frequency of digit # is equal to its value or not if (i != occ[i]): print("No"); return; print("Yes"); N = 3331; check(N); # This code is contributed by saurabh_jaiswal.
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG { // Function to check if the number // is valid or not public static void check(int N) { // Stores the frequencies // of digits Dictionary<int, int> occ = new Dictionary<int, int>(); while (N > 0) { // Extracting the digits int r = N % 10; if (occ.ContainsKey(r)) { // If char is present in charCountMap, // incrementing it's count by 1 occ[r] = occ[r] + 1; } else { // If char is not present in charCountMap, // putting this char to charCountMap with 1 // as it's value occ.Add(r, 1); } N = N / 10; } foreach(int key in occ.Keys) { if (key != occ[key]) { Console.Write("NO"); return; } } Console.Write("Yes"); } // Driver code public static void Main() { int N = 3331; check(N); } } // This code is contributed by Saurabh Jaiswal
Javascript
<script> // Javascript program for the above approach // Function to check if the number // is valid or not function check(N) { // Stores the frequencies // of digits let occ = new Map(); while (N > 0) { // Extracting the digits let r = N % 10; // Incrementing the frequency if(occ.has(r)){ occ.set(r, occ.get(r) + 1) }else{ occ.set(r, 1) } N = Math.floor(N / 10); } // Iterating the hashmap for (i of occ) { // Check if frequency of digit // is equal to its value or not if (i[0] != i[1]) { document.write("No<br>"); return; } } document.write("Yes<br>"); } let N = 3331; check(N); </script>
Yes
Complejidad de tiempo : O(D), D = número de dígitos en N
Espacio auxiliar : O(D)
Publicación traducida automáticamente
Artículo escrito por mihikasingh1501 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA