A lot of difficult problems lately.. how are you guys holding up?
@PeterPan-xe7qw Жыл бұрын
Excited to catch up with them all! 😄
@ajaygonepuri2285 Жыл бұрын
Watching your videos helps a lot and this being Exam season in India couldn't use more than an hour on leetcode so directly to your solutions.
@ary_21 Жыл бұрын
by breaking the streak , medium + dp is enough for my brain for 2 days . This and yesterdays one was a nightmare
@steeve1 Жыл бұрын
after this, i went back to 2-sum to re-establish my dominance
@m.kamalali Жыл бұрын
hey neetcode could you help me figure out why my code give me TLE mod=10**9 + 7 cnt=defaultdict(int) for w in words: for i,c in enumerate(w): cnt[(i,c)]+=1 dp={} def dfs(i,k): if i==len(target): return 1 if k==len(words[0]): return 0 if (i,k) in dp: return dp[(i,k)] dp[(i,k)]=0 for j in range (k,len(words[0])): c=cnt[(j,target[i])] if c>0: dp[(i,k)]+=c*dfs(i+1,j+1) return dp[(i,k)]%mod return dfs(0,0)
@raginibhayana8305 Жыл бұрын
I have a little request. when you explain dp problems can you start with the recursive approach, then memoized and then further a tabulisation approach.
@ReD4eva948 ай бұрын
Hmm, he already did so much. May be just do the tabulation part yourself?
@nizarkadri3109 Жыл бұрын
I am noticing that almost all recursive problems have a more efficient iterative approach, it would be great if you can also include the iterative approach in videos. Thanks a lot for helping with these problems
@tunguyenxuan8296 Жыл бұрын
Recursive is basically pushing and popping from a stack iteratively. If you cant think about any other iterative solution, just go ahead and try it using stack.
@uptwist2260 Жыл бұрын
Thank you for the video solution. I got a TLE and couldn't figure out what important optimization lee215 was doing. You pointed it out for me, which was storing the count of every character for each position of the words before recursion. Thanks to you, I will come up with this optimization myself next time.
@saketsoni2587 Жыл бұрын
optimised my code after watching the code upto 6:48, it worked!
@spicy2112 Жыл бұрын
One kind request. Please use intuitive variable names. Your explanation is top-notch. However, it's really easy to follow along if 'c' was 'current_target_letter' and 'k' was 'col_index'
@dinzonnyuoe Жыл бұрын
I got stucked in the recursion logic. really great explanation Mr. Neetcode :)
@pawandeepchor89 Жыл бұрын
Great solution!! just one optimization if cnt[(k,c)] != 0: dp[(i, k)] += cnt[(k,c)] * dfs(i + 1, k + 1) i.e. if cnt[(k,c)] is 0 why bother calling the dfs.
@nw3052 Жыл бұрын
After solving a bunch of DP problems, DFS solutions come to me very quickly, and the crux in them is optimization. However, the real challenge is to come up with an iterative tabulation solution. It's hard to figure out how to propagate numbers between cells, and I'd like to see a tabulation solution as well to such problems.
@JeffreyConcerto Жыл бұрын
So helpful. Thank you. You really did a great job making this problem seem easy.
@mahimahans111 Жыл бұрын
Great explanation for a tough problem! Thanks.
@deepanshsharma4342 Жыл бұрын
If we loop through the strings to count the matching characters, it timed out for me. I guess it is necessary to precompute the count of characters at each index.
@akashdey5637 Жыл бұрын
why multiplied by 2? check timestamp-->6:38
@MP-ny3ep Жыл бұрын
great explanation as always !
@ouchlock Жыл бұрын
awesome as always
@liangyu3771 Жыл бұрын
you saved my day
@vikneshcs48246 ай бұрын
Cam we construct a graph and do dfs
@metin4yt Жыл бұрын
Do we need to have a dict for cnt? Can't we just use an array of len(target)? If the character in w does not match the corresponding character in k, do we need to count that?
@itachid Жыл бұрын
The thing I don't understand in this is how are we checking if the character at that particular index in the word matches the character at that index in the target?
@ZeonLP Жыл бұрын
The check is done implicitly by using the „cnt“ map. If target[i] = c, then cnt[(k, c)] = 0 if there is no match, i.e. there is no word w with w[k] = c. Otherwise at least one word has character c at index k. Note that we could choose any of them for a new way to form the target string.
@igorf2439 ай бұрын
good one
@mohithadiyal6083 Жыл бұрын
Just great 😃👍
@ADITYA-fk1zy Жыл бұрын
java version: class Solution { private int n; private int l; private int m; private String t; private static Long[][] dp; private static int[][] cnt; private int MOD = (int)1e9 + 7; public long dfs(int i,int k) { if(k == m) return 1; if(i == l) return 0; if(dp[i][k] != null) { return dp[i][k]; } dp[i][k] = dfs(i + 1,k); int c = t.charAt(k); if(cnt[i][c - 'a'] != 0) { dp[i][k] += (cnt[i][c - 'a']) * dfs(i + 1,k + 1); dp[i][k] %= MOD; } return dp[i][k]; } public int numWays(String[] words, String target) { t = target; n = words.length; l = words[0].length(); m = target.length(); dp = new Long[l][m]; cnt = new int[l][26]; for(int i = 0;i < l;i++) { for(int j = 0;j < n;j++) { cnt[i][words[j].charAt(i) - 'a'] += 1; } } return (int)dfs(0,0); } }