Skip to content

Commit a18feaa

Browse files
committed
2019-09-03
1 parent 1d4370b commit a18feaa

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution(object):
2+
def numWays(self, n, k):
3+
"""
4+
:type n: int
5+
:type k: int
6+
:rtype: int
7+
"""
8+
dp = [0] * (n + 3)
9+
dp[0], dp[1], dp[2] = 0, k, k * k
10+
for i in range(3, n + 1):
11+
dp[i] = dp[i - 1] * (k - 1) + dp[i - 2] * (k - 1)
12+
return dp[n]

5176.猜字谜/5176-猜字谜.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ def findNumOfValidWords(self, words, puzzles):
88
from collections import Counter
99
for i in range(len(words)):
1010
words[i] = "".join(sorted(set(words[i])))
11-
record = Counter(words) #统计每种单词出现的频率
11+
record = Counter(words) #统计每种pattern出现的频率
1212

1313
res = []
1414
for p in puzzles:
15-
bfs = [p[0]]
15+
bfs = [p[0]] #固定首字母
1616
for char in p[1:]:
17-
bfs += [s + char for s in bfs]
17+
bfs += [s + char for s in bfs] #生成64种可能
1818
cnt = 0
1919
for combination in bfs:
2020
tmp = "".join(sorted(combination))
21-
if tmp in record:
21+
if tmp in record: #看看当前pattern在words里有没有出现过
2222
cnt += record[tmp]
2323
res.append(cnt)
2424
return res

0 commit comments

Comments
 (0)