Count Vowels Permutation - Dynamic Programming - Leetcode 1220 - Python

Sdílet
Vložit
  • čas přidán 7. 09. 2024

Komentáře • 114

  • @NeetCode
    @NeetCode  Před 2 lety +5

    🚀 neetcode.io/ - The best FREE site to prepare for Coding Interviews
    🥷 Discord: discord.gg/ddjKRXPqtk
    🐦 Twitter: twitter.com/neetcode1

    • @udaykiransugali1139
      @udaykiransugali1139 Před 2 lety

      Thanks you sir..

    • @anujkhare3815
      @anujkhare3815 Před 2 lety

      Hey can you please make video on leetcofe 395 : longest substring with atleast k repeating characters , and show both approaches, Divide and conquer as well as sliding window

  • @CarlJohnson-iv7sn
    @CarlJohnson-iv7sn Před 2 lety +62

    The return of the king.

  • @QazJer
    @QazJer Před 2 lety +44

    The drawings + the way you mapped the ending letters to an index made this problem really easy to understand

    • @NeetCode
      @NeetCode  Před 2 lety +2

      Glad it was helpful!

    • @HeremHD
      @HeremHD Před 2 lety +1

      @@NeetCode You're providing us with so much value. Thanks for providing us with these high level concepts. I'm in my second year of college and the resources you're providing give me a ton of hope for the future in terms of understanding these concepts

  • @sirmidor
    @sirmidor Před 2 lety +22

    Since in each step we only need to "look back" at the results of the previous iteration and no further back, you can do this with just one array, as long as you use unpacking (multiple temp variables are possible too) so that newly calculated values do not interfere with calculating other new values:
    arr = [1, 1, 1, 1, 1]
    for _ in range(n - 1):
    arr[0], arr[1], arr[2], arr[3], arr[4] = (
    arr[1],
    arr[0] + arr[2],
    arr[0] + arr[1] + arr[3] + arr[4],
    arr[2] + arr[4],
    arr[0]
    )
    return sum(arr) % ((10 ** 9) + 7)

  • @habashyjr
    @habashyjr Před 2 lety +26

    The O(1) memory solution:
    def countVowelPermutation(self, n: int) -> int:
    a, e, i, o, u = 1, 1, 1, 1, 1
    for _ in range(n - 1):
    a, e, i, o, u = e + i + u, a + i, e + o, i, i + o
    return (a + e + i + o + u) % (10**9 + 7)

    • @ruthviks
      @ruthviks Před 10 měsíci +1

      Man. The solution works. Awesome!

  • @charziken500
    @charziken500 Před 2 lety +6

    Hi, thanks a lot for these videos! I remember how I used to be terrified of leetcode (still am a bit haha) but your approach and thought process in these videos have rubbed off on me and massively improved my confidence during my job search. I’ve now got an offer from GS 😊

  • @arpanbanerjee6224
    @arpanbanerjee6224 Před 2 lety +8

    When I see that you have put up a video explaining any problem, I am so sure that I will understand it. This peace of mind we get. :)

  • @fdk9913
    @fdk9913 Před 2 lety +5

    The legend has came back

  • @akshitdayal2689
    @akshitdayal2689 Před 2 lety +1

    You're the best Neetcode!!!! Everytime I search for a leetcode question on youtube and I see that Neetcode has a video on it, I'm the happiest person coz your explanation is the besttt!!! Thank you so much for everything that you do

  • @mohithadiyal6083
    @mohithadiyal6083 Před 2 lety +2

    Again the most simplest and amazing explanation ever on CZcams 👍🏻👍🏻😁😁

  • @md.shazidalhasan6726
    @md.shazidalhasan6726 Před 2 lety +4

    My very first hard problem 😂 . Thanks

  • @doublegdog
    @doublegdog Před 2 lety

    Neetcode the best code. Landed a job at amazon thanks to grinding supplemented with your videos but I still like watching your solutions even though I dont need them anymore haha. Keep up the great work!

  • @awepossum1059
    @awepossum1059 Před rokem

    Such a complex problem, yet you make it so easy to understand! It almost seems obvious at the end!

  • @kyriakoskourkoulis1159
    @kyriakoskourkoulis1159 Před 2 lety +2

    Man ur amazing! I just saw the problem (Daily Challenge) and ur video popped up right after!

  • @darkjemdude
    @darkjemdude Před 2 lety

    Got this question as my daily challenge literally 2 hours after you put up this video. Great timing.

  • @sugandhm2666
    @sugandhm2666 Před 2 lety +3

    Java coders in line 16 of this code make sure u mod two times to avoid overflow and incorrect answers.
    We have to put mod after addition of every two terms and adjust brackets accordingly.
    dp[j][a] = ( (dp[j-1][e] + dp[j-1][i]) % mod + dp[j-1][u] ) % mod

    • @CostaKazistov
      @CostaKazistov Před 2 lety +1

      Brilliant.
      That's what had me stumped why it was failing when testing n = 144.
      Was expecting 18208803, but instead was getting totally wrong result, while other tests passed.
      Thank you!

  • @infinitygod5379
    @infinitygod5379 Před 2 lety +1

    Just until a week ago i didn't even know practical dynamic programming. Watched few vids and now I came up with an ans in a minute after pausing you vid at 1:16 and now i have to see if my ans is correct before checking out entire video

  • @pironobcoding
    @pironobcoding Před 2 lety +1

    you make all the hard question look easy great job 👍

  • @lawrencejustin8136
    @lawrencejustin8136 Před 2 lety +2

    Thanks alot Neet! It appears you have mixed o for u. O can be followed by i and u while u is followed by i. Also, when the question says n7mber of strings that could be formed by n length, how does your n=2 example fit in? I was thinking each of the characters can only form strings of length n only that the condition holds. In your example, I didn5 see the relationship between the 5 characters and n.

  • @estifanosbireda1892
    @estifanosbireda1892 Před 2 lety

    the table is more than enough to understand the solution. tnks, as always, Happy Sunday!

  • @simonlitvinskii1898
    @simonlitvinskii1898 Před 2 lety +1

    Since all formulas are linear, you can rewrite step as matrix multiplication and then use fast exponentiation to reach O(log n) solution, and it still would be O(1) memory

  • @jlpeng9036
    @jlpeng9036 Před 2 lety

    your explain is so good. everytime i watch your video i feel i can beat all hard leetcode questions and get to google offer. but everytime i actually do the hard question on leetcode, i still struggle. haha

  • @DeGoya
    @DeGoya Před 2 lety +1

    I think a good idea for this channel would be to also engage with system design interview questions

  • @yan0kyan0
    @yan0kyan0 Před 2 lety

    Thank you so much! Well explained. I recommended your channel to my friends. Plus you inspire me a lot. Even though I work for a great company, and our product is used by millions, we have customers such as Google, Apple, etc, I still dream about becoming a Googler.

  • @lopotun
    @lopotun Před 2 lety

    You explain comlpicated things in so simple and clear way 👍 Thank you!

  • @YT.Nikolay
    @YT.Nikolay Před 2 lety +1

    Why dp problems are so complicated? :( Thanks for the video, happy and thankful you keep making new videos!

  • @CostaKazistov
    @CostaKazistov Před 2 lety +1

    Timely video! Daily LeetCode Challenge
    Thanks :)

  • @charanbathula984
    @charanbathula984 Před 2 lety +2

    Can be memory efficient if used only two rows in dp where updating 2nd row using 1st row and making both rows equal at end of each iteration

  • @shrimpo6416
    @shrimpo6416 Před 2 lety

    My idea is to do dfs with caching. Yours is to think backward and cnt the number of str end with certain letter and make the dp arr much easier to understand! Love it!

  • @Vishal_84_k
    @Vishal_84_k Před 2 lety +1

    So long time I saw your vedio😉😉😉 but good time with you💓💓

  • @programmingrush
    @programmingrush Před 2 lety

    As previous state is only required, the array is not necessary making it O(1) or constant space solution.

  • @KaushikPal
    @KaushikPal Před 2 lety +3

    It was a long waiting

  • @mohammadwahiduzzamankhan4397

    Your Leetcode tutorials are the best.

  • @DeGoya
    @DeGoya Před 2 lety

    I'm a Java developer but your videos are the most helpful on CZcams when it comes to LeetCode problems. Do you think it's enough to understand and solve Blind 79 to pass a job interview?

  • @karthik1627
    @karthik1627 Před 2 lety

    Using hash maps would make the time complexity better, because insert operation is O(1) in hash maps whereas in list the insert operation is O(n)

  • @yaswanthkosuru
    @yaswanthkosuru Před 2 lety +1

    king is back

  • @ChanChan-pg4wu
    @ChanChan-pg4wu Před 2 lety

    return of the lord of leetcode!

  • @sumanthpola5030
    @sumanthpola5030 Před 2 lety

    Congratulations 👏 for the 200k family 🥳😍

  • @ngneerin
    @ngneerin Před 2 lety +2

    Same solution with exponentially lower space complexity
    a, e, i, o, u = 1, 1, 1, 1, 1
    for _ in range(n-1):
    a, e, i, o, u = (e + i + u), (a + i), (e + o), i, (i + o)
    return (a + e + i + o + u) % 1000000007

    • @NeetCode
      @NeetCode  Před 2 lety

      Nice! Best part of python is not having to worry about overflow

    • @ngneerin
      @ngneerin Před 2 lety

      I find that an issue while preparing for interview. Interviewer could be from java background and would be expecting solution to include methods to avoid overflowing

  • @varunshrivastava2706
    @varunshrivastava2706 Před 2 lety

    You have a very soothing voice.

  • @yaxlu
    @yaxlu Před 2 lety +1

    Thanks!

  • @billyfung
    @billyfung Před 2 lety

    Thanks a lot. You are really a good teacher.

  • @orionblaze7812
    @orionblaze7812 Před 2 lety

    Congrats on 200K

  • @uditsharma5688
    @uditsharma5688 Před 2 lety

    As always, Thank you for the great explanation.
    I was actually thinking to work on my understanding for these ending char dp questions . Nice that leetcode chose this question for today's daily challenge , and you chose to upload a video for this.

    • @varunshrivastava2706
      @varunshrivastava2706 Před 2 lety

      I am thinking about starting to learn dynamic programming. Any resources that you can suggest me?

    • @uditsharma5688
      @uditsharma5688 Před 2 lety

      @@varunshrivastava2706 just start with leetcode easy dp questions.

    • @varunshrivastava2706
      @varunshrivastava2706 Před 2 lety

      @@uditsharma5688 By resources I meant CZcams videos!

    • @uditsharma5688
      @uditsharma5688 Před 2 lety

      @@varunshrivastava2706 you could follow striver and geeksforgeeks CZcams channel. And neetcode is there on top of the list.

  • @acerf129
    @acerf129 Před 2 lety +1

    I used dfs and @cache.It actually works ,I don't know if it's appropriate sometimes to solve dp problem like this?

    • @NeetCode
      @NeetCode  Před 2 lety +1

      Yeah that also works!

    • @memeproductions4182
      @memeproductions4182 Před 2 lety

      how did you do it?did you just cache the count for each ith char in the string together with the last char?

    • @taloto07
      @taloto07 Před 7 měsíci

      @@memeproductions4182 here is a recursive solution in java
      class Solution {
      Map map;
      int mod;
      Map cache;
      public int countVowelPermutation(int n) {
      mod = 1000000000 + 7;
      cache = new HashMap();
      map = new HashMap();
      map.put('a', Arrays.asList('e'));
      map.put('e', Arrays.asList('a', 'i'));
      map.put('i', Arrays.asList('a', 'e', 'o', 'u'));
      map.put('o', Arrays.asList('i', 'u'));
      map.put('u', Arrays.asList('a'));
      int count = 0;
      for(char c: map.keySet()){
      count = (count + count(n - 1, c)) % mod;
      }
      return count;
      }
      private int count(int n, char c){
      if (n == 0) return 1;
      String key = n + "" + c;
      if (cache.containsKey(key))
      return cache.get(key);
      int count = 0;
      for(char nc: map.get(c)){
      count = (count + count(n - 1, nc)) % mod;
      }
      cache.put(key, count);
      return cache.get(key);
      }
      }

  • @hamzashabbir3589
    @hamzashabbir3589 Před 2 lety +1

    King is back 👑

  • @niteshmanem1997
    @niteshmanem1997 Před 2 lety +2

    NEET IS BACK AT IT AGAIN WITH A BANGER SOLUTION. NEET FOR PRESIDENT!!! Waiting for the day for Sundar to retire from Google and we get NEET as CEO. Until that day, I will continue to comment subscribe like share his videos until Sundar forcefully retires and gives his seat to Neet

  • @zergenzerg6853
    @zergenzerg6853 Před 2 lety

    Neetcode staying prepped up for a senior position
    When are you going to do Sys Design ?

  • @basma-ba
    @basma-ba Před 2 lety

    thank you for this vide. could you please make the " 419 - battleship in a board " problem. You're the only one I understand his explanation. Thank you a lot for this valuable channel

  • @amadoufall6237
    @amadoufall6237 Před 2 lety

    Very clear explanation, as usual. Could you please tackle 1386. Cinema Seat Allocation? I would love to see your solution

  • @brettm5289
    @brettm5289 Před 2 lety

    This is next level

  • @ngneerin
    @ngneerin Před 2 lety

    Such a beautiful combination problem

  • @adarshsinghrathore7424

    What a brilliant solution.

  • @thedianacalhoun
    @thedianacalhoun Před rokem

    What was the reason for adding the mod function?

  • @refertechno
    @refertechno Před 2 lety

    Dude ...
    @4:50
    Could you pls tell me about how you came about the reverse logic?

  • @jaxzhang5002
    @jaxzhang5002 Před 2 lety

    Can you do LC (721 I think) Merge Accounts? Or if you’ve already done one with similar structure, can you link?

  • @ilhammukati2493
    @ilhammukati2493 Před 2 lety

    can you try to get the grind 75 all into your videos!

  • @SuperSLAMBear
    @SuperSLAMBear Před 2 lety

    Ok real question, after working at google for sometimes now, have you actually use any of the hard/tricky data structure & algorithms in job?

  • @mangola-x8u
    @mangola-x8u Před 2 lety

    Can you make a playlist using javscript as well?

  • @summaiyaparveen8730
    @summaiyaparveen8730 Před 2 lety

    Could you please make a video on cherry pickup problem(leetcode)

  • @siddhant997
    @siddhant997 Před 2 lety

    Wow welcome back!

  • @govindbanura3327
    @govindbanura3327 Před 2 lety

    Can someone pls explain the mod thing? I didn't get that.

  • @osaid56
    @osaid56 Před 2 lety

    Super Thanks

  • @jarjarbinks8954
    @jarjarbinks8954 Před 2 lety

    Missed you man

  • @arpanbanerjee6224
    @arpanbanerjee6224 Před 2 lety

    Boss is back!

  • @shake-her3908
    @shake-her3908 Před 2 lety

    Thank u!

  • @johnconnor9787
    @johnconnor9787 Před 7 měsíci

    Amazing

  • @ngneerin
    @ngneerin Před 2 lety

    Missed your solutions

  • @habashyjr
    @habashyjr Před 2 lety

    You can always mod result only, no need to mod each and every one

    • @NeetCode
      @NeetCode  Před 2 lety +1

      I think for some languages it would cause an overflow

  • @behzat1398
    @behzat1398 Před 11 měsíci

    what name of this ide ?

  • @gokusaiyan1128
    @gokusaiyan1128 Před 2 lety

    Do race car question leetcode 818

  • @oxyht
    @oxyht Před 2 lety

    Hi neetcode, just saw you website. I think I am the first to saw the changes. Can you give some discount to your Indian user Rs10000($129) is a lot. Can you make it Rs 5000. I will buy it in a giffy.

  • @user-gq1ij
    @user-gq1ij Před 2 lety

    Easiest hard question

  • @kennethkath6527
    @kennethkath6527 Před 2 lety

    Is it only me that makes me think he sounds like Colt Steele?

  • @lifeshuk
    @lifeshuk Před 2 lety +1

    My brain is not for DP 😂

  • @md.shazidalhasan6726
    @md.shazidalhasan6726 Před 2 lety

    Can't you use a hash map Instead?

  • @Notaarav6876
    @Notaarav6876 Před 2 lety +1

    Hello

  • @Ash-fo4qs
    @Ash-fo4qs Před 2 lety

    can someone give the c++ code

    • @raunaqsingh875
      @raunaqsingh875 Před 2 lety

      int countVowelPermutation(int n) {
      if(n==1) return 5;
      if(n==2) return 10;
      long a=1,e=1,i=1,o=1,u=1,mod=1e9+7;
      long a2,e2,i2,o2,u2;
      for(int j=2;j

    • @kyriakoskourkoulis1159
      @kyriakoskourkoulis1159 Před 2 lety

      Yeah, you can write it this way:
      #define NO_CHARS 5
      #define MOD 1'000'000'007
      #define ll long long
      class Solution {
      public:
      int countVowelPermutation(int n) {
      ll dp[n+1][NO_CHARS];
      // base case
      for (int j = 0; j < NO_CHARS; ++j) dp[1][j] = 1;
      int a = 0, e = 1, i = 2, o = 3, u = 4;
      for (int j = 2; j

  • @aishikdalui_0734
    @aishikdalui_0734 Před 2 lety

    #I think this is o(1) solution
    class Solution:
    def countVowelPermutation(self, n: int) -> int:
    a, e, i, o, u = 1, 1, 1, 1, 1
    for _ in range(2, n + 1):
    a, e, i, o, u = e + i + u, a + i, e + o, i, i + o

    return (a + e + i + o + u) % 1000000007

  • @Notaarav6876
    @Notaarav6876 Před 2 lety +4

    I quit being a software engineer