Kth Distinct String in an Array - Leetcode 2053 - Python

  Рет қаралды 5,898

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 28
@gmh14
@gmh14 Ай бұрын
lol, your thumbnails are always interesting. you added a guitar cause the problem has "string" in its name xD
@chrischika7026
@chrischika7026 Ай бұрын
In python dictionaries are Ordered after 3.7
@warrendragnir2363
@warrendragnir2363 Ай бұрын
They are.
@31redorange08
@31redorange08 Ай бұрын
That seems wasteful.
@yohelln7644
@yohelln7644 Ай бұрын
I just started doing leetcode recently as a beginner. Today I managed to do this problem at the first try and not in the worst way! I'm pretty happy right now :)
@jeffhappens1
@jeffhappens1 Ай бұрын
Yay, I thought of and coded up the better approach for the first time. Thank you again for making these videos.
@IntelliStar_
@IntelliStar_ Ай бұрын
FYI, dictionaries have a .get() method which takes the default value as a second parameter. So you can use it like count[s] = count.get(s, 0) + 1, if you don't want to use if statement.
@WhosShamouz
@WhosShamouz Ай бұрын
Yey ;) Happy for teh same solution :D class Solution: def kthDistinct(self, arr: List[str], k: int) -> str: mapping = Counter(arr) for key, value in mapping.items(): if value == 1: k -= 1 if k == 0: return key return ""
@alexanderp7521
@alexanderp7521 Ай бұрын
Life is easier with Counter )
@pastori2672
@pastori2672 Ай бұрын
i remember when i couldn't solve a hashmap problem in the google dsa course thingy and would always refer to you and be so confused how you knew it's a hashmap problem :) good times
@Amy-601
@Amy-601 Ай бұрын
LibkedHashMap in Java would keep/ retain the initial order and act like a map. But python 🐍 is easier/ better. I like this one better.
@michaelroditis1952
@michaelroditis1952 Ай бұрын
you can iterate on the dictionary because in python the order is preserved
@raphaelboakye5545
@raphaelboakye5545 Ай бұрын
For some reason, using two hashsets has a slight better performance than using a hashmap in Java. Can you explain why that?
@hardiksrivastava9174
@hardiksrivastava9174 Ай бұрын
how would you do this using 2 hashsets ?
@blackbeard3449
@blackbeard3449 Ай бұрын
​@@hardiksrivastava9174create two hashsets, one to keep track of distinct elements and another to keep track of duplicate solutions, the second method in the editorial does this
@leeroymlg4692
@leeroymlg4692 Ай бұрын
I wouldn't trust leetcode metrics for comparing performance. Run the same code multiple times and you'll get different speeds
@raphaelboakye5545
@raphaelboakye5545 Ай бұрын
@@hardiksrivastava9174 public String kthDistinct(String[] arr, int k) { Set distinct = new HashSet(); Set duplicates = new HashSet(); for(String word : arr){ if(!distinct.add(word)) duplicates.add(word); } for(String word : arr){ if(duplicates.add(word)) k--; if(k == 0) return word; } return ""; }
@utkarshdewan8736
@utkarshdewan8736 Ай бұрын
If you are going by the leetcode runtime then better not because they are very random
@juanmacias5922
@juanmacias5922 Ай бұрын
I guess dicts do keep the order the element was inserted? LOL class Solution: def kthDistinct(self, arr: List[str], k: int) -> str: count = Counter(arr) for char, cnt in count.items(): if cnt == 1: k -= 1 if k == 0: return char return ""
@abhimanyuambastha2595
@abhimanyuambastha2595 Ай бұрын
Dicts keep the order in which the elements are entered. Dont assume it to be sorted, will cause mistakes. Use SortedContainer when needs to be sorted
@juanmacias5922
@juanmacias5922 Ай бұрын
@@abhimanyuambastha2595 oh yeah, I guess I should have stated insertion order! xD C++ does have a sorted map, and an unordered_map though :D
@chrischika7026
@chrischika7026 Ай бұрын
This is True.
@abhimanyuambastha2595
@abhimanyuambastha2595 Ай бұрын
@@juanmacias5922 Yes C has that and for python we have sortedcontainer.SortedDict and sortedcontainer.SortedList and I believe java has TreeMap
@hrithikbandaru6462
@hrithikbandaru6462 Ай бұрын
return [i for i in arr if Counter(arr)[i] == 1][k-1] if len([i for i in arr if Counter(arr)[i] == 1]) >= k else ""
@dion9795
@dion9795 Ай бұрын
why does this even need 2 solutions
@utkarshdewan8736
@utkarshdewan8736 Ай бұрын
Useful in an interview. Starting with brute force and then moving to more optimized one. Although this question is very simple that for me as well only the second one came to mind
@MP-ny3ep
@MP-ny3ep Ай бұрын
You have to understand that there are people who are just starting out. This approach is way better than simply explaining the optimized solution.
@chrischika7026
@chrischika7026 Ай бұрын
what do you mean ? the more the merrier.
Lemonade Change - Leetcode 860 - Python
10:37
NeetCodeIO
Рет қаралды 7 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 511 М.
АЗАРТНИК 4 |СЕЗОН 1 Серия
40:47
Inter Production
Рет қаралды 1,4 МЛН
Amazing Parenting Hacks! 👶✨ #ParentingTips #LifeHacks
00:18
Snack Chat
Рет қаралды 15 МЛН
My Techlead Roasted Me
10:24
NeetCodeIO
Рет қаралды 127 М.
Creators of Devin AI are genius competitive programmers?
20:23
NeetCodeIO
Рет қаралды 182 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 305 М.
Programmers in 2024 have no Deep Knowledge
8:38
NeetCodeIO
Рет қаралды 182 М.
Regions Cut By Slashes - Leetcode 959 - Python
16:06
NeetCodeIO
Рет қаралды 16 М.
Maximum Number of Points with Cost - Leetcode 1937 - Python
15:15
АЗАРТНИК 4 |СЕЗОН 1 Серия
40:47
Inter Production
Рет қаралды 1,4 МЛН