5
I have the following problem:
Given an input n
I have to calculate the number of possible permutations with the digits that compose it. For example, if n = 123
I have 6 numbers that can be formed: 123
, 132
, 231
, 213
, 312
, 321
. But if the entrance is 100
the answer would be 1, since the possible permutations are: 001
, 010
and 001
but 001
, 010
are not valid representations for the problem.
Applying the mathematical formula for permutation with repetition, I arrived at the following code:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Similaridade {
public int solucao(int n) {
List<Integer> digitos = separarDigitos(n);
Map<Integer, Integer> ocorrencias;
Integer resultado;
Integer[] valores;
ocorrencias = this.contarOcorrencias(digitos);
valores = ocorrencias.values().toArray(new Integer[ocorrencias.size()]);
resultado = this.calcularPermutacao(digitos.size(), valores);
return resultado;
}
public Integer calcularPermutacao(Integer total, Integer... repeticoes) {
Long denominador = 1L;
Long numerador;
Long resultado;
for (Integer repeticao : repeticoes) {
if (repeticao > 1) {
denominador = denominador * this.fatorial(repeticao);
}
}
numerador = this.fatorial(total);
resultado = numerador / denominador;
return resultado.intValue();
}
private List<Integer> separarDigitos(int numero) {
List<Integer> resultado = new ArrayList<>();
while (numero != 0) {
int digito = numero % 10;
numero = numero / 10;
resultado.add(digito);
}
return resultado;
}
private Map<Integer, Integer> contarOcorrencias(List<Integer> numeros) {
Map<Integer, Integer> ocorrencias = new HashMap<>();
numeros.forEach((numero) -> {
if (ocorrencias.containsKey(numero)) {
ocorrencias.put(numero, ocorrencias.get(numero) + 1);
} else {
ocorrencias.put(numero, 1);
}
});
return ocorrencias;
}
private Long fatorial(Integer numero) {
Long resultado = 1L;
for (int fator = 2; fator <= numero; fator++) {
resultado = resultado * fator;
}
return resultado;
}
}
But now I need to calculate and apply the results with zeros.
How can I do that? Remembering that performance is important and that, therefore, just perform the exchanges to have all the possibilities is not interesting. The resolution is more complete by applying the discount in the formula or after the calculation of the formula.
Examples:
- Entree:
1213
. Exit:12
. - Entree:
123
. Exit:6
. - Entree:
100
. Exit:1
. - Entree:
120
. Exit:4
. - Entree:
1200
. Exit:6
. - Entree:
0
. Exit:1
.
Is there a time limit for running the program ? If yes, how much ? There are restrictions for the
n
? If yes which?– Isac
@Isac limit does not exist, but making one by one every possibility I already have ready. The interesting thing would be to solve by discovering only the combinations starting with
0
mathematically to discount the result of the current code.– Sorack
In other words, your idea is that by some mathematical deduction you can get to the number of unique numbers by discounting numbers with zeros on the left, without having to iterate on each one ?
– Isac
@Isac yes, exactly
– Sorack