Skip to content

Commit 53f06f7

Browse files
committed
Runtime: 1401 ms (Top 5.55%) | Memory: 48.5 MB (Top 63.89%)
1 parent fb7180b commit 53f06f7

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1+
// Runtime: 1401 ms (Top 5.55%) | Memory: 48.5 MB (Top 63.89%)
2+
13
//""This solution isn`t the best way but it`s simple""
24
var numTilePossibilities = function(tiles) {
35
return search(tiles);
4-
};
5-
// this function takes a state and check if it`s valid or !
6+
};
7+
// this function takes a state and check if it`s valid or !
68
// valid state is not null state and unique
79
// e.g ["","A","A"]=>is not valid state because is has null state and repeated
8-
// values by contrast ["A"] is valid state
10+
// values by contrast ["A"] is valid state
911
// !!state below is to check if state not null
1012
const isValidState=(state="",res=[])=>!!state&&!res.includes(state);
11-
// this function permutes tiles by backtracking
13+
// this function permutes tiles by backtracking
1214
const search=(tiles="",state='',result=[])=>{
13-
if(isValidState(state,result))//check if the current state valid
15+
if(isValidState(state,result))//check if the current state valid
1416
result.push(state)
15-
//loop through tiles and every time you push letter to state
17+
//loop through tiles and every time you push letter to state
1618
// the remaining tiles will decreased by one
1719
/**
1820
* ---e.g--
19-
* level 1 tiles=AAB state=""
20-
* it will loop through the tiles ;
21-
* now I am in index 0 push tiles[0] to state now state=A
22-
* by making this operation number of option tiles will be
23-
* decreased to newTiles="AB"
24-
* ...
25-
* ..
26-
* .
27-
* */
21+
* level 1 tiles=AAB state=""
22+
* it will loop through the tiles ;
23+
* now I am in index 0 push tiles[0] to state now state=A
24+
* by making this operation number of option tiles will be
25+
* decreased to newTiles="AB"
26+
* ...
27+
* ..
28+
* .
29+
* */
2830
for(let i=0;i<tiles.length;i++){
2931
state+=tiles[i];
3032
const newTiles=tiles.substring(0,i)+tiles.substring(i+1);
3133
search(newTiles,state,result)
3234
state=state.slice(0,state.length-1);
33-
}
35+
}
3436
return result.length;
35-
}
37+
}

0 commit comments

Comments
 (0)