Count Vowels Permutation - Dynamic Programming - Leetcode 1220 - Python

Поділитися
Вставка
  • Опубліковано 8 чер 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🥷 Discord: / discord
    🐦 Twitter: / neetcode1
    🐮 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    Problem Link: leetcode.com/problems/count-v...
    0:00 - Read the problem
    1:20 - Drawing Explanation
    8:02 - Coding Explanation
    leetcode 1220
    #dynamic #programming #python
    Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
  • Наука та технологія

КОМЕНТАРІ • 115

  • @NeetCode
    @NeetCode  Рік тому +5

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

    • @udaykiransugali1139
      @udaykiransugali1139 Рік тому

      Thanks you sir..

    • @anujkhare3815
      @anujkhare3815 Рік тому

      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 Рік тому +61

    The return of the king.

  • @QazJer
    @QazJer Рік тому +44

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

    • @NeetCode
      @NeetCode  Рік тому +2

      Glad it was helpful!

    • @HeremHD
      @HeremHD Рік тому +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 Рік тому +21

    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 Рік тому +23

    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 7 місяців тому +1

      Man. The solution works. Awesome!

  • @fdk9913
    @fdk9913 Рік тому +5

    The legend has came back

  • @arpanbanerjee6224
    @arpanbanerjee6224 Рік тому +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. :)

  • @akshitdayal2689
    @akshitdayal2689 Рік тому +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

  • @pironobcoding
    @pironobcoding Рік тому +1

    you make all the hard question look easy great job 👍

  • @mohithadiyal6083
    @mohithadiyal6083 Рік тому +2

    Again the most simplest and amazing explanation ever on UA-cam 👍🏻👍🏻😁😁

  • @md.shazidalhasan6726
    @md.shazidalhasan6726 Рік тому +4

    My very first hard problem 😂 . Thanks

  • @darkjemdude
    @darkjemdude Рік тому

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

  • @kyriakoskourkoulis1159
    @kyriakoskourkoulis1159 Рік тому +2

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

  • @doublegdog
    @doublegdog Рік тому

    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!

  • @estifanosbireda1892
    @estifanosbireda1892 Рік тому

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

  • @CostaKazistov
    @CostaKazistov Рік тому +1

    Timely video! Daily LeetCode Challenge
    Thanks :)

  • @charziken500
    @charziken500 Рік тому +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 😊

  • @lopotun
    @lopotun Рік тому

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

  • @mohammadwahiduzzamankhan4397

    Your Leetcode tutorials are the best.

  • @awepossum1059
    @awepossum1059 Рік тому

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

  • @billyfung
    @billyfung Рік тому

    Thanks a lot. You are really a good teacher.

  • @sumanthpola5030
    @sumanthpola5030 Рік тому

    Congratulations 👏 for the 200k family 🥳😍

  • @yankhonski
    @yankhonski Рік тому

    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.

  • @jlpeng9036
    @jlpeng9036 Рік тому

    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

  • @orionblaze7812
    @orionblaze7812 Рік тому

    Congrats on 200K

  • @varunshrivastava2706
    @varunshrivastava2706 Рік тому

    You have a very soothing voice.

  • @ChanChan-pg4wu
    @ChanChan-pg4wu Рік тому

    return of the lord of leetcode!

  • @__vishal__8788
    @__vishal__8788 Рік тому +1

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

  • @hamzashabbir3589
    @hamzashabbir3589 Рік тому +1

    King is back 👑

  • @siddhant997
    @siddhant997 Рік тому

    Wow welcome back!

  • @DeGoya
    @DeGoya Рік тому +1

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

  • @yaxlu
    @yaxlu Рік тому +1

    Thanks!

  • @vinit.khandelwal
    @vinit.khandelwal Рік тому

    Such a beautiful combination problem

  • @adarshsinghrathore7424
    @adarshsinghrathore7424 Рік тому

    What a brilliant solution.

  • @infinitygod5379
    @infinitygod5379 Рік тому +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

  • @KaushikPal
    @KaushikPal Рік тому +3

    It was a long waiting

  • @shrimpo6416
    @shrimpo6416 Рік тому

    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!

  • @yaswanthkosuru
    @yaswanthkosuru Рік тому +1

    king is back

  • @uditsharma5688
    @uditsharma5688 Рік тому

    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 Рік тому

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

    • @uditsharma5688
      @uditsharma5688 Рік тому

      @@varunshrivastava2706 just start with leetcode easy dp questions.

    • @varunshrivastava2706
      @varunshrivastava2706 Рік тому

      @@uditsharma5688 By resources I meant UA-cam videos!

    • @uditsharma5688
      @uditsharma5688 Рік тому

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

  • @lawrencejustin8136
    @lawrencejustin8136 Рік тому +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.

  • @YT.Nikolay
    @YT.Nikolay Рік тому +1

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

  • @brettm5289
    @brettm5289 Рік тому

    This is next level

  • @shake-her3908
    @shake-her3908 Рік тому

    Thank u!

  • @jarjarbinks8954
    @jarjarbinks8954 Рік тому

    Missed you man

  • @simonlitvinskii1898
    @simonlitvinskii1898 Рік тому +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

  • @sugandhm2666
    @sugandhm2666 Рік тому +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 Рік тому +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!

  • @DeGoya
    @DeGoya Рік тому

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

  • @seascrew7358
    @seascrew7358 Рік тому

    Thanks much and please stay free! I've never sent a gift to an online but you deserve it (feel special:) )

  • @osaid56
    @osaid56 Рік тому

    Super Thanks

  • @arpanbanerjee6224
    @arpanbanerjee6224 Рік тому

    Boss is back!

  • @johnconnor9787
    @johnconnor9787 4 місяці тому

    Amazing

  • @charanbathula984
    @charanbathula984 Рік тому +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

  • @amadoufall6237
    @amadoufall6237 Рік тому

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

  • @refertechno
    @refertechno Рік тому

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

  • @programmingrush
    @programmingrush Рік тому

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

  • @rohit21061993
    @rohit21061993 Рік тому

    Can you make a playlist using javscript as well?

  • @jaxzhang5002
    @jaxzhang5002 Рік тому

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

  • @SuperSLAMBear
    @SuperSLAMBear Рік тому

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

  • @thedianacalhoun
    @thedianacalhoun Рік тому

    What was the reason for adding the mod function?

  • @basma-ba
    @basma-ba Рік тому

    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

  • @acerf129
    @acerf129 Рік тому +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  Рік тому +1

      Yeah that also works!

    • @memeproductions4182
      @memeproductions4182 Рік тому

      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 4 місяці тому

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

  • @summaiyaparveen8730
    @summaiyaparveen8730 Рік тому

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

  • @karthik1627
    @karthik1627 Рік тому

    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)

  • @vinit.khandelwal
    @vinit.khandelwal Рік тому

    Missed your solutions

  • @niteshmanem1997
    @niteshmanem1997 Рік тому +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

  • @govindbanura3327
    @govindbanura3327 Рік тому

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

  • @ilhammukati2493
    @ilhammukati2493 Рік тому

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

  • @zergenzerg6853
    @zergenzerg6853 Рік тому

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

  • @behzat1398
    @behzat1398 8 місяців тому

    what name of this ide ?

  • @vinit.khandelwal
    @vinit.khandelwal Рік тому +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  Рік тому

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

    • @vinit.khandelwal
      @vinit.khandelwal Рік тому

      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

  • @gokusaiyan1128
    @gokusaiyan1128 Рік тому

    Do race car question leetcode 818

  • @habashyjr
    @habashyjr Рік тому

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

    • @NeetCode
      @NeetCode  Рік тому +1

      I think for some languages it would cause an overflow

  • @user-gq1ij
    @user-gq1ij Рік тому

    Easiest hard question

  • @md.shazidalhasan6726
    @md.shazidalhasan6726 Рік тому

    Can't you use a hash map Instead?

  • @lifeshuk
    @lifeshuk Рік тому +1

    My brain is not for DP 😂

  • @Ash-fo4qs
    @Ash-fo4qs Рік тому

    can someone give the c++ code

    • @raunaqsingh875
      @raunaqsingh875 Рік тому

      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 Рік тому

      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

  • @kennethkath6527
    @kennethkath6527 Рік тому

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

  • @Monkey45139
    @Monkey45139 Рік тому +1

    Hello

  • @oxyht
    @oxyht Рік тому

    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.

  • @aishikdalui_0734
    @aishikdalui_0734 Рік тому

    #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

  • @Monkey45139
    @Monkey45139 Рік тому +4

    I quit being a software engineer