Created
July 18, 2020 00:19
-
-
Save germanescobar/edf49093d4062a3ad33f2e7e826f7df8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numTilePossibilities = function(tiles) { | |
return rec(tiles) | |
}; | |
function rec(tiles) { | |
if (tiles.length === 1) { | |
return 1 | |
} | |
let sum = 0 | |
for (let i=0; i < tiles.length; i++) { | |
let result = rec(tiles.slice(0,i) + tiles.slice(i+1)) | |
sum += result + 1 | |
} | |
return sum | |
} |
Sabía que estaba muy cerca, la solución refactorizada es la siguiente:
var numTilePossibilities = function(tiles) {
if (tiles.length === 1) return 1
const visited = {}
let sum = 0
for (let i=0; i < tiles.length; i++) {
let current = tiles[i]
if (!visited[current]) {
let result = numTilePossibilities(tiles.slice(0,i) + tiles.slice(i+1))
sum += result + 1
visited[current] = true
}
}
return sum
};
Sólo necesitábamos ir guardando las letras y no repetir el proceso para las duplicadas!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Funciona cuando no hay letras repetidas. ¿Qué cambios necesitamos para que funcione con letras repetidas?