1
+ // Runtime: 1401 ms (Top 5.55%) | Memory: 48.5 MB (Top 63.89%)
2
+
1
3
//""This solution isn`t the best way but it`s simple""
2
4
var numTilePossibilities = function ( tiles ) {
3
5
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 !
6
8
// valid state is not null state and unique
7
9
// 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
9
11
// !!state below is to check if state not null
10
12
const isValidState = ( state = "" , res = [ ] ) => ! ! state && ! res . includes ( state ) ;
11
- // this function permutes tiles by backtracking
13
+ // this function permutes tiles by backtracking
12
14
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
14
16
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
16
18
// the remaining tiles will decreased by one
17
19
/**
18
20
* ---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
+ * */
28
30
for ( let i = 0 ; i < tiles . length ; i ++ ) {
29
31
state += tiles [ i ] ;
30
32
const newTiles = tiles . substring ( 0 , i ) + tiles . substring ( i + 1 ) ;
31
33
search ( newTiles , state , result )
32
34
state = state . slice ( 0 , state . length - 1 ) ;
33
- }
35
+ }
34
36
return result . length ;
35
- }
37
+ }
0 commit comments