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

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ต.ค. 2024

ความคิดเห็น • 32

  • @NeetCodeIO
    @NeetCodeIO  ปีที่แล้ว +38

    A lot of difficult problems lately.. how are you guys holding up?

    • @PeterPan-xe7qw
      @PeterPan-xe7qw ปีที่แล้ว

      Excited to catch up with them all! 😄

    • @ajaygonepuri2285
      @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
      @ary_21 ปีที่แล้ว

      by breaking the streak , medium + dp is enough for my brain for 2 days . This and yesterdays one was a nightmare

    • @steeve1
      @steeve1 ปีที่แล้ว +7

      after this, i went back to 2-sum to re-establish my dominance

    • @m.kamalali
      @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
    @raginibhayana8305 ปีที่แล้ว +18

    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.

    • @ReD4eva94
      @ReD4eva94 5 หลายเดือนก่อน

      Hmm, he already did so much. May be just do the tabulation part yourself?

  • @nizarkadri3109
    @nizarkadri3109 ปีที่แล้ว +6

    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
      @tunguyenxuan8296 ปีที่แล้ว +1

      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
    @uptwist2260 ปีที่แล้ว +1

    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
    @saketsoni2587 ปีที่แล้ว

    optimised my code after watching the code upto 6:48, it worked!

  • @spicy2112
    @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
    @dinzonnyuoe ปีที่แล้ว +1

    I got stucked in the recursion logic. really great explanation Mr. Neetcode :)

  • @JeffreyConcerto
    @JeffreyConcerto ปีที่แล้ว

    So helpful. Thank you. You really did a great job making this problem seem easy.

  • @nw3052
    @nw3052 ปีที่แล้ว +1

    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.

  • @mahimahans111
    @mahimahans111 ปีที่แล้ว

    Great explanation for a tough problem! Thanks.

  • @pawandeepchor89
    @pawandeepchor89 ปีที่แล้ว +1

    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.

  • @deepanshsharma4342
    @deepanshsharma4342 ปีที่แล้ว +1

    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
    @akashdey5637 ปีที่แล้ว +1

    why multiplied by 2? check timestamp-->6:38

  • @vikneshcs4824
    @vikneshcs4824 3 หลายเดือนก่อน

    Cam we construct a graph and do dfs

  • @metin4yt
    @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?

  • @MP-ny3ep
    @MP-ny3ep ปีที่แล้ว

    great explanation as always !

  • @igorf243
    @igorf243 6 หลายเดือนก่อน

    good one

  • @liangyu3771
    @liangyu3771 ปีที่แล้ว

    you saved my day

  • @ouchlock
    @ouchlock ปีที่แล้ว

    awesome as always

  • @itachid
    @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
      @ZeonLP ปีที่แล้ว +2

      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.

  • @mohithadiyal6083
    @mohithadiyal6083 ปีที่แล้ว

    Just great 😃👍

  • @ADITYA-fk1zy
    @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);
    }
    }