1
//""This solution isn`t the best way but it`s simple""2
var numTilePossibilities = function (tiles) {5
// this function takes a state and check if it`s valid or !6
// valid state is not null state and unique7
// e.g ["","A","A"]=>is not valid state because is has null state and repeated8
// values by contrast ["A"] is valid state9
// !!state below is to check if state not null10
const isValidState = (state = "", res = []) => !!state && !res.includes(state);11
// this function permutes tiles by backtracking12
const search = (tiles = "", state = "", result = []) => {13
if (isValidState(state, result))14
//check if the current state valid16
//loop through tiles and every time you push letter to state17
// the remaining tiles will decreased by one20
* level 1 tiles=AAB state=""21
* it will loop through the tiles ;22
* now I am in index 0 push tiles[0] to state now state=A23
* by making this operation number of option tiles will be24
* decreased to newTiles="AB"29
for (let i = 0; i < tiles.length; i++) {31
const newTiles = tiles.substring(0, i) + tiles.substring(i + 1);32
search(newTiles, state, result);33
state = state.slice(0, state.length - 1);