Skip to content

Commit 72822aa

Browse files
authored
Update letter-tile-possibilities.py
1 parent 57a924d commit 72822aa

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Python/letter-tile-possibilities.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,24 @@ def numTilePossibilities(self, tiles):
4141
for i in xrange(1, len(coeff)):
4242
result += int(round(coeff[i]*fact[i]))
4343
return result
44+
45+
46+
# Time: O(r), r is the value of result
47+
# Space: O(n)
48+
class Solution2(object):
49+
def numTilePossibilities(self, tiles):
50+
"""
51+
:type tiles: str
52+
:rtype: int
53+
"""
54+
def backtracking(counter):
55+
total = 0
56+
for k, v in counter.iteritems():
57+
if not v:
58+
continue
59+
counter[k] -= 1
60+
total += 1+backtracking(counter)
61+
counter[k] += 1
62+
return total
63+
64+
return backtracking(collections.Counter(tiles))

0 commit comments

Comments
 (0)