Skip to content

Commit 39a357b

Browse files
committed
Dec 29
1 parent 27c5cf0 commit 39a357b

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from collections import Counter
2+
from functools import lru_cache
3+
from typing import List
4+
5+
6+
class Solution:
7+
def numWays(self, words: List[str], target: str) -> int:
8+
count = [Counter() for _ in range(len(words[0]))]
9+
for word in words:
10+
for idx, ch in enumerate(word):
11+
count[idx][ch] += 1
12+
13+
@lru_cache(None)
14+
def dfs(i: int = 0, j: int = 0) -> int:
15+
if i == len(target):
16+
return 1
17+
if j == len(words[0]):
18+
return 0
19+
return (dfs(i, j+1) + dfs(i+1, j+1) * count[j][target[i]]) % int(1e9 + 7)
20+
21+
return dfs()
22+
23+
24+
def main():
25+
words = ['acca', 'bbbb', 'caca']
26+
target = 'aba'
27+
assert Solution().numWays(words, target) == 6
28+
29+
words = ['abba', 'baab']
30+
target = 'bab'
31+
assert Solution().numWays(words, target) == 4
32+
33+
34+
if __name__ == '__main__':
35+
main()

2024-12-December-LeetCoding-Challenge/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
| December 25 | [515. Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/) | Medium | Solved |
3131
| December 26 | [494. Target Sum](https://leetcode.com/problems/target-sum/) | Medium | Solved |
3232
| December 27 | [1014. Best Sightseeing Pair](https://leetcode.com/problems/best-sightseeing-pair/) | Medium | Unsolved |
33-
| December 28 | []() | | |
34-
| December 29 | []() | | |
33+
| December 28 | [689. Maximum Sum of 3 Non-Overlapping Subarrays](https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/) | Hard | Unsolved |
34+
| December 29 | [1639. Number of Ways to Form a Target String Given a Dictionary](https://leetcode.com/problems/number-of-ways-to-form-a-target-string-given-a-dictionary/) | Hard | Unsolved |
3535
| December 30 | []() | | |
3636
| December 31 | []() | | |
3737

@@ -40,4 +40,4 @@
4040
| --- | --- | --- | --- |
4141
| Easy | 5 | 5 | 0 |
4242
| Medium | 19 | 12 | 7 |
43-
| Hard | 3 | 0 | 3 |
43+
| Hard | 5 | 0 | 5 |

0 commit comments

Comments
 (0)