Skip to content

Commit

Permalink
Dec 29
Browse files Browse the repository at this point in the history
  • Loading branch information
siddydutta committed Dec 29, 2024
1 parent 27c5cf0 commit 39a357b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from collections import Counter
from functools import lru_cache
from typing import List


class Solution:
def numWays(self, words: List[str], target: str) -> int:
count = [Counter() for _ in range(len(words[0]))]
for word in words:
for idx, ch in enumerate(word):
count[idx][ch] += 1

@lru_cache(None)
def dfs(i: int = 0, j: int = 0) -> int:
if i == len(target):
return 1
if j == len(words[0]):
return 0
return (dfs(i, j+1) + dfs(i+1, j+1) * count[j][target[i]]) % int(1e9 + 7)

return dfs()


def main():
words = ['acca', 'bbbb', 'caca']
target = 'aba'
assert Solution().numWays(words, target) == 6

words = ['abba', 'baab']
target = 'bab'
assert Solution().numWays(words, target) == 4


if __name__ == '__main__':
main()
6 changes: 3 additions & 3 deletions 2024-12-December-LeetCoding-Challenge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
| December 25 | [515. Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/) | Medium | Solved |
| December 26 | [494. Target Sum](https://leetcode.com/problems/target-sum/) | Medium | Solved |
| December 27 | [1014. Best Sightseeing Pair](https://leetcode.com/problems/best-sightseeing-pair/) | Medium | Unsolved |
| December 28 | []() | | |
| December 29 | []() | | |
| December 28 | [689. Maximum Sum of 3 Non-Overlapping Subarrays](https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/) | Hard | Unsolved |
| 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 |
| December 30 | []() | | |
| December 31 | []() | | |

Expand All @@ -40,4 +40,4 @@
| --- | --- | --- | --- |
| Easy | 5 | 5 | 0 |
| Medium | 19 | 12 | 7 |
| Hard | 3 | 0 | 3 |
| Hard | 5 | 0 | 5 |

0 comments on commit 39a357b

Please sign in to comment.