lol, your thumbnails are always interesting. you added a guitar cause the problem has "string" in its name xD
@chrischika7026Ай бұрын
In python dictionaries are Ordered after 3.7
@warrendragnir2363Ай бұрын
They are.
@31redorange08Ай бұрын
That seems wasteful.
@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Ай бұрын
Yay, I thought of and coded up the better approach for the first time. Thank you again for making these videos.
@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Ай бұрын
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Ай бұрын
Life is easier with Counter )
@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Ай бұрын
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Ай бұрын
you can iterate on the dictionary because in python the order is preserved
@raphaelboakye5545Ай бұрын
For some reason, using two hashsets has a slight better performance than using a hashmap in Java. Can you explain why that?
@hardiksrivastava9174Ай бұрын
how would you do this using 2 hashsets ?
@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Ай бұрын
I wouldn't trust leetcode metrics for comparing performance. Run the same code multiple times and you'll get different speeds
@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Ай бұрын
If you are going by the leetcode runtime then better not because they are very random
@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Ай бұрын
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Ай бұрын
@@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Ай бұрын
This is True.
@abhimanyuambastha2595Ай бұрын
@@juanmacias5922 Yes C has that and for python we have sortedcontainer.SortedDict and sortedcontainer.SortedList and I believe java has TreeMap
@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Ай бұрын
why does this even need 2 solutions
@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Ай бұрын
You have to understand that there are people who are just starting out. This approach is way better than simply explaining the optimized solution.