-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathletter-tiles-possibilities.js
More file actions
52 lines (33 loc) · 936 Bytes
/
Copy pathletter-tiles-possibilities.js
File metadata and controls
52 lines (33 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
*/
var numTilePossibilities = function(tiles) {
let result = new Set();
let flag = [];
let s = "";
const backtrack = (tiles) => {
for(let i = 0; i < tiles.length; i++) {
if(flag[ i ] != 1) {
s += tiles[ i ];
flag[ i ] = 1;
result.add(s);
backtrack(tiles);
flag[ i ] = 0;
s = s.slice(0, -1);
}
}
};
backtrack(tiles);
return (result.size);
};