Dado un número N , la tarea es eliminar los duplicados adyacentes de un número N y luego ordenar los dígitos del número N en orden decreciente.
Ejemplo:
Entrada: N=11123134
Salida: 433211Entrada: N=22133455
Salida: 54321
Enfoque: siga los pasos a continuación para resolver el siguiente problema:
- Cree un mapa para almacenar la frecuencia de todos los caracteres, digamos mp .
- Cree una función getDigits que aceptará un número entero como parámetro y lo devolverá en forma de string.
- Itere en esa string y solo incremente la frecuencia de los dígitos que no tienen un duplicado en su índice anterior.
- Iterar en el mapa mp y crear una string de enteros, que se ordenarán automáticamente.
- Convierta esa string en entero e imprima la respuesta en consecuencia.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ code for the above approach #include <bits/stdc++.h> using namespace std; // Function to change integer N to string string getDigits(int N) { return to_string(N); } // Function to remove the adjacent duplicates // of a number N and then sort the digits // of the number N in decreasing order int getSolution(int N) { string s = getDigits(N); if (s.size() == 1) { return stoi(s); } map<int, int> mp; for (int i = 1; i < s.size(); ++i) { if (s[i] != s[i - 1]) { mp[s[i - 1] - '0']++; } } mp[s[s.size() - 1] - '0']++; // Creating the number int mul = 1; int ans = 0; for (auto x : mp) { for (int i = 0; i < x.second; i++) { ans += x.first * mul; mul *= 10; } } // Returning the created number return ans; } // Driver Code int main() { int N = 11123134; cout << getSolution(N); }
Java
// Java code for the above approach import java.util.*; class GFG{ // Function to change integer N to String static String getDigits(int N) { return String.valueOf(N); } // Function to remove the adjacent duplicates // of a number N and then sort the digits // of the number N in decreasing order static int getSolution(int N) { char []s = getDigits(N).toCharArray(); if (s.length == 1) { return Integer.valueOf(String.valueOf(s)); } HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>(); for (int i = 1; i < s.length; ++i) { if (s[i] != s[i - 1]) { if(mp.containsKey(s[i - 1] - '0')){ mp.put(s[i - 1] - '0', mp.get(s[i - 1] - '0')+1); } else mp.put(s[i - 1] - '0', 1); } } if(mp.containsKey(s[s.length - 1] - '0')){ mp.put(s[s.length - 1] - '0', mp.get(s[s.length - 1] - '0')+1); } else mp.put(s[s.length - 1] - '0', 1); // Creating the number int mul = 1; int ans = 0; for (Map.Entry<Integer,Integer> x : mp.entrySet()) { for (int i = 0; i < x.getValue(); i++) { ans += x.getKey() * mul; mul *= 10; } } // Returning the created number return ans; } // Driver Code public static void main(String[] args) { int N = 11123134; System.out.print(getSolution(N)); } } // This code is contributed by 29AjayKumar
Python3
# Python code for the above approach # Function to change integer N to string def getDigits(N) : return str(N); # Function to remove the adjacent duplicates # of a number N and then sort the digits # of the number N in decreasing order def getSolution(N): s = getDigits(N); if (len(s) == 1): return int(s); mp = {} for i in range(1, len(s)): if (s[i] != s[i - 1]): if ((ord(s[i - 1]) - ord('0')) in mp): mp[ord(s[i - 1]) - ord('0')] += 1; else: mp[ord(s[i - 1]) - ord('0')] = 1; if (s[-1] in mp): mp[ord(s[-1]) - ord('0')] += 1 else: mp[ord(s[-1]) - ord('0')] = 1 # Creating the number mul = 1; ans = 0; for x in mp: for i in range(mp[x]): ans = ans + x * mul; mul = mul * 10; # Returning the created number return ans; # Driver Code N = 11123134; print(getSolution(N)); # This code is contributed by gfgking
C#
// C# code for the above approach using System; using System.Collections.Generic; class GFG{ // Function to change integer N to String static string getDigits(int N) { return (N).ToString(); } // Function to remove the adjacent duplicates // of a number N and then sort the digits // of the number N in decreasing order static int getSolution(int N) { char[] s = getDigits(N).ToCharArray(); if (s.Length == 1) { return Int32.Parse((s).ToString()); } Dictionary<int, int> mp = new Dictionary<int, int>(); for(int i = 1; i < s.Length; ++i) { if (s[i] != s[i - 1]) { if (mp.ContainsKey(s[i - 1] - '0')) { mp[s[i - 1] - '0']++; } else mp[s[i - 1] - '0'] = 1; } } if (mp.ContainsKey(s[s.Length - 1] - '0')) { mp[s[s.Length - 1] - '0'] += 1; } else mp[s[s.Length - 1] - '0'] = 1; // Creating the number int mul = 1; int ans = 0; foreach(KeyValuePair<int, int> x in mp) { for(int i = 0; i < x.Value; i++) { ans += x.Key * mul; mul *= 10; } } // Returning the created number return ans; } // Driver Code public static void Main(string[] args) { int N = 11123134; Console.WriteLine(getSolution(N)); } } // This code is contributed by ukasp
Javascript
<script> // JavaScript code for the above approach // Function to change integer N to string function getDigits(N) { return N.toString(); } // Function to remove the adjacent duplicates // of a number N and then sort the digits // of the number N in decreasing order function getSolution(N) { let s = getDigits(N); if (s.length == 1) { return parseInt(s); } let mp = new Map(); for(let i = 1; i < s.length; ++i) { if (s[i] != s[i - 1]) { if (mp.has(s[i - 1].charCodeAt(0) - '0'.charCodeAt(0))) { mp.set(s[i - 1].charCodeAt(0) - '0'.charCodeAt(0), mp.get(s[i - 1].charCodeAt(0) - '0'.charCodeAt(0)) + 1); } else { mp.set(s[i - 1].charAt(0) - '0'.charAt(0), 1); } } } if (mp.has(s[s.length - 1])) mp.set(s[s.length - 1].charCodeAt(0) - '0'.charCodeAt(0), mp.get(s[s.length - 1].charCodeAt(0) - '0'.charCodeAt(0)) + 1); else mp.set(s[s.length - 1].charCodeAt(0) - '0'.charCodeAt(0), 1) // Creating the number let mul = 1; let ans = 0; for(let [key, value] of mp) { for(let i = 0; i < value; i++) { ans = ans + key * mul; mul = mul * 10; } } // Returning the created number return ans; } // Driver Code let N = 11123134; document.write(getSolution(N)); // This code is contributed by Potta Lokesh </script>
Producción
433211
Complejidad temporal: O(logN)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por revathymsony9 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA