Skip to content

Commit 0211c86

Browse files
committed
[LeetCode Sync] Runtime - 29 ms (71.39%), Memory - 27 MB (7.42%)
1 parent b6a74c8 commit 0211c86

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<p>You have <code>n</code>&nbsp;&nbsp;<code>tiles</code>, where each tile has one letter <code>tiles[i]</code> printed on it.</p>
2+
3+
<p>Return <em>the number of possible non-empty sequences of letters</em> you can make using the letters printed on those <code>tiles</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> tiles = &quot;AAB&quot;
10+
<strong>Output:</strong> 8
11+
<strong>Explanation: </strong>The possible sequences are &quot;A&quot;, &quot;B&quot;, &quot;AA&quot;, &quot;AB&quot;, &quot;BA&quot;, &quot;AAB&quot;, &quot;ABA&quot;, &quot;BAA&quot;.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> tiles = &quot;AAABBC&quot;
18+
<strong>Output:</strong> 188
19+
</pre>
20+
21+
<p><strong class="example">Example 3:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> tiles = &quot;V&quot;
25+
<strong>Output:</strong> 1
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= tiles.length &lt;= 7</code></li>
33+
<li><code>tiles</code> consists of uppercase English letters.</li>
34+
</ul>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def numTilePossibilities(self, tiles: str) -> int:
3+
res = set() # all solutions
4+
5+
def dfs(path, t):
6+
if path not in res:
7+
if path:
8+
res.add(path)
9+
for i in range(len(t)):
10+
dfs(path+t[i], t[:i] + t[i+1:])
11+
12+
dfs('', tiles) # start dfs with empty path (init)
13+
return len(res)

0 commit comments

Comments
 (0)