Solution Blog: (sign into leetcode to view)
- https://leetcode.com/problems/number-of-ways-to-form-a-target-string-given-a-dictionary/solutions/3423636/intuitive-python-solution-solution-blog/
First! We build a lookup array where the ith index maps to the ith column. At every cell we will store the number of occurences every letter appears at that ith column.
Next! We create our recursive call, cache it with @cache and pass in i to indicate the current index of the target letter we're try to build & c the current column we're considering.
Two Base Cases!!
We run out of letters in our target --- return 1, this is a valid path!
We run out of columns --- return 0, we weren't able to construct our target
Two recursive Calls!!
Skip the current column, move on to the next!
Use the number of paths we can go down at the current column, and iterate to the next call and next target letter!
I hope my code helps a little - have an awesome day!
Download
0 formats
No download links available.
Number of Ways to Form a Target String Given a Dictionary - LeetCode 1639 - Python Solution | NatokHD