Number of Ways to Form a Target String Given a Dictionary - LeetCode 1639 - Python Solution

preview_player
Показать описание
Solution Blog: (sign into leetcode to view)

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!
Рекомендации по теме
Комментарии
Автор

Hey man, great job on the video! Just a thought, you might want to look into removing the echo with free AI software to make ur vids even better. One of the ones I've heard about is called Descript and their Studio Sound feature

juanfernandez
Автор

it was so satisfying getting this problem right, it took me one TLE to come up with the lookup table idea. also, why do you use @cache instead of a 2d dp[i][c] where you save the result? is there ever a difference?

brady