Find Common Characters - Leetcode 1002 - Python

Sdílet
Vložit
  • čas přidán 24. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
    🐦 Twitter: / neetcode1
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    Problem Link: leetcode.com/problems/find-co...
    0:00 - Read the problem
    0:30 - Drawing Explanation
    7:20 - Coding Explanation
    leetcode 1002
    #neetcode #leetcode #python
  • Věda a technologie

Komentáře • 37

  • @Juicysalad07
    @Juicysalad07 Před měsícem +12

    I had exactly this approach, took me some time to think of it though and I wasn't sure if it was optimal or not. I guess it was

  • @chiragjoshi482
    @chiragjoshi482 Před měsícem +24

    weird question. easier to solve, harder to code imo .

    • @chrischika7026
      @chrischika7026 Před měsícem

      if you know python its pretty easy.

    • @kinnetikira
      @kinnetikira Před měsícem

      @@chrischika7026python is the Very Easy path for most of these

  • @adityamwagh
    @adityamwagh Před měsícem +7

    I basically made a 26 x num_words array i.e. a row for each alphabet, and then for each row I calculated the minimum value in the row, and added min_value * char to the result array. So basically a O(26 * n) time and O(26 * n) space solution. I was worrying about the zero but turns out I didn't need that character if it's count is zero!

    • @hida-steak-donburi
      @hida-steak-donburi Před měsícem

      Same here!

    • @TechNinjain720p
      @TechNinjain720p Před měsícem

      The worst case runtime should be O(n * m) where n = num_words and m = avg. length of a word? If your filling those rows

  • @MP-ny3ep
    @MP-ny3ep Před měsícem +2

    Great explanation again. Thank you.

  • @twlight9000
    @twlight9000 Před měsícem +3

    Just started to look at this problem and you just posted the video

  • @BillyL6
    @BillyL6 Před měsícem +1

    I created a table for the first word with letters as the key and the values as the count. Then for each subsequent word, if the letter exists, I replaced the value in the first table with the minimum count of the new table and the first table. If the letter doesn't exist, I set the key's value to 0 in the first table, using it as a flag that it doesn't exist in all tables. Once you have the first table fully completed, there should be 0s which indicate that a letter doesn't exist in all the dictionaries. Then you iterate through the first table and anything that is a value > 0 means it exists in all tables, so you just for loop through it and generate the answer using the keys.

  • @AnordinaryMan007
    @AnordinaryMan007 Před měsícem +5

    I thought the same but coding this was kinda hard in cpp considering this is just an easy question

    • @Dannnneh
      @Dannnneh Před měsícem +2

      Surprised that std doesn't have a find_nth function.

    • @AnordinaryMan007
      @AnordinaryMan007 Před měsícem +2

      @@Dannnneh you have to implement these by yourself that's why I don't like cpp for DSA.

    • @dhruvagrawal8286
      @dhruvagrawal8286 Před měsícem

      yeah . I think I should start learning python

  • @vijethkashyap151
    @vijethkashyap151 Před měsícem

    Learnt we can iterate over Counters directly instead of doing dict.items(). Also that while iterating over counter, it doesn't give key errors and just assigns count to zero in case the key wasn't found! Pretty NEET I must say!

  • @athiyamanonep4453
    @athiyamanonep4453 Před měsícem

    I did exactly the same way, except i used tmp hash map to get min count values from cnt and curcnt and put it back in cnt .lol i could have avoided it .

  • @freecourseplatformenglish2829

    I solved it using 2 dict instead of Counter. I prefer close to native rather than using fancy Counter.

  • @chien-yuyeh9386
    @chien-yuyeh9386 Před měsícem

    🥳🥳👋 Thanks for sharing!

  • @salmanrayeen8693
    @salmanrayeen8693 Před měsícem +1

    Dude plz also do solution in C++..........

  • @adnanelouadghiri6880
    @adnanelouadghiri6880 Před měsícem

    Perfect ❤

  • @benedictaamoah5310
    @benedictaamoah5310 Před měsícem +1

    Hii thank you for the video today,your tutorials are super helpful and it's always nice watching you code out your solutions. I coded out a simpler solution that I wanted to share with you if you don't mind that I think is shorter and more efficient. I'm open to feedback as well
    class Solution:
    def commonChars(self, words: List[str]) -> List[str]:
    results = []
    for char in set(words[0]):
    count = min(word.count(char) for word in words)
    results += [char] * count
    return results

  • @tunno4586
    @tunno4586 Před měsícem +1

    is using a counter the same as using 2 hashmaps?
    class Solution:
    def commonChars(self, words: List[str]) -> List[str]:
    n = len(words)
    res = []
    h1 = defaultdict(int)
    for i in range(n):
    h2 = h1
    h1 = defaultdict(int)
    for j in range(len(words[i])):
    h1[words[i][j]] += 1
    if i > 0:
    for c in h1:
    h1[c] = min(h1[c], h2[c])
    for c in h1:
    while h1[c] > 0:
    res.append(c)
    h1[c] -= 1
    return res

  • @sagar5251
    @sagar5251 Před měsícem +2

    How can this be an 'easy' problem, is it just hard for me or what?

  • @RubyVA-gx4cd
    @RubyVA-gx4cd Před měsícem

    Ok, I was able to do it in O(n square), and O(n) space, let's see how I should do it the right way

  • @Munchen888
    @Munchen888 Před měsícem

    I made thus:
    counter = Counter(words[0])
    for i in range(1, len(words)):
    counter_2 = Counter(words[i])
    counter &= counter_2
    return counter.elements()

  • @aemswe
    @aemswe Před měsícem +1

    I do not fully understand why the space complexty is 1, can anyone clarify?

    • @magdeline1207
      @magdeline1207 Před měsícem

      same, thought cnt hashmap uses space

    • @rahuluppuluri7489
      @rahuluppuluri7489 Před měsícem

      It's because the counter dictionary will at most have 26 keys (the number of letters in the alphabet), meaning its space complexity is O(26), which in other words is constant and therefore O(1) space

  • @lakshyadhawan800
    @lakshyadhawan800 Před měsícem +1

    it would be great, if you would post the solution in other languages too , in the description maybe, your explainations are great

  • @DeathSugar
    @DeathSugar Před měsícem +1

    worst thing that it returns array of strings instead of array of characters. the biggest performance eater out there

  • @kahafb
    @kahafb Před měsícem

    class Solution:
    def commonChars(self, words: List[str]) -> List[str]:
    count = Counter(words[0])
    for i in range(1, len(words)):
    count &= Counter(words[i])
    return count.elements()

  • @Zmey5656
    @Zmey5656 Před měsícem

    My time is 7 ms and I beat 39.77% of users with Go.

  • @bradleyhanson
    @bradleyhanson Před měsícem +2

    Not recommended, but lol python:
    class Solution:
    def commonChars(self, words: List[str]) -> List[str]:
    return reduce(and_, map(Counter, words)).elements()

  • @s8x.
    @s8x. Před měsícem

    can u mentor me

  • @user-qp5cw8sd4w
    @user-qp5cw8sd4w Před měsícem

    Why does neetcode always sound angry 😅

  • @yashsolanki069
    @yashsolanki069 Před měsícem

    Rip coding this in js