Ransom Note - Leetcode 383 - Hashmaps & Sets (Python)

  Рет қаралды 10,844

Greg Hogg

Greg Hogg

Күн бұрын

Пікірлер: 32
@GregHogg
@GregHogg 5 ай бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@anshpradhan1954
@anshpradhan1954 5 ай бұрын
in my opinion, your roadmap website and the videos you put out are so under rated, your content is A level, and so far the best tutor of DSA in python in the entire community i have encountered, you are doing great work Greg 👍👍👍
@RojinSharma-c8l
@RojinSharma-c8l 7 ай бұрын
Great explanation! Very well explained. Loved how you explained even the different approach, and sticked with the one that's best for the interviews. Thank you
@boringhuman9427
@boringhuman9427 3 ай бұрын
dict.get(value,defalult value) - It can eliminate the requirement of defaultdict
@spider-capgaming1487
@spider-capgaming1487 4 ай бұрын
Is it better to have a dictionary of the characters in magazine and loop over ransomNote or vice versa or is it basically the same?
@venzdrop
@venzdrop 4 ай бұрын
There is no exactly one solution to a problem. There are multiple solutions to solve one problem(I'm not saying every single problem because some problems require only one solution, and some require a lot of solutions). Rule number -> "if the code works, don't touch it." Yes, you can. Have a good day.
@gaganlonde7190
@gaganlonde7190 5 ай бұрын
why can you store the chars of magazine in a list and loop over the chars of RansomNote and check if they in list if yes remove it from the list and if not return false as the ransomNote cant be made?
@venzdrop
@venzdrop 4 ай бұрын
yes, you can. i tried your question and coded it and it worked for the first three examples of leetcode I tried on vscode, but I didn't submit on leetcode yet. imma do it later. did it work for you?. There is no exactly one solution to a problem. There are multiple solutions to solve one problem(I'm not saying every single problem because some problems require only one solution, and some require a lot of solutions). Rule number -> "if the code works, don't touch it." Yes, you can. Have a good day.
@aki_blasco
@aki_blasco 6 ай бұрын
Wouldn't space complexity be O(m) as you said in the video? Because it would be the number of key-value pairs from the counter that can hold at most O(m) unique characters from the magazine. I do not get why it would be O(1) space complexity.
@Harry-em7km
@Harry-em7km 5 ай бұрын
It is O(26) at most if magazine has every letter in the alphabet, so we can just call it constant. Reason it's not O(m) is because even if magazine is a really long string (m >> 26), the counter will still only ever have 26 keys at most
@user_sense
@user_sense 8 ай бұрын
where do you get these problems from?
@GregHogg
@GregHogg 8 ай бұрын
They're all from leetcode
@hlubradio2318
@hlubradio2318 7 ай бұрын
His brain? No. Leetcode
@venzdrop
@venzdrop 4 ай бұрын
@@hlubradio2318 lol
@MrIrishBoss
@MrIrishBoss 4 ай бұрын
Keeping it simple, just want to make sure there are enough of each letter in the magazine. for i in set(ransomNote): if magazine.count(i) < ransomNote.count(i): return False return True
@mo_1023
@mo_1023 4 ай бұрын
While it's more elegant and concise, it's an inefficient solution that has a time complexity of O(n * m) .. because magazine.count(i) searches the entire string in O(n), and then for every letter in ransom_note, your doing that magazine.count(i) .. if the length of ransom_note is m, then we have O(n * m) .. for large text it will consume a lot of memory and run slow
@mjanish9836
@mjanish9836 8 ай бұрын
Good Explanation !! ThankYou
@GregHogg
@GregHogg 8 ай бұрын
Glad to hear it! You're very welcome :)
@Ivan-Shyriaiev
@Ivan-Shyriaiev 2 ай бұрын
Thanks 🫡
@lakshyamathur8808
@lakshyamathur8808 8 ай бұрын
Couldn't it be solved using substring method ??
@GregHogg
@GregHogg 8 ай бұрын
What's that?
@lakshyamathur8808
@lakshyamathur8808 8 ай бұрын
@@GregHogg isn't there a method to check if string A is substring of string B?
@zfarahx
@zfarahx 8 ай бұрын
@@lakshyamathur8808 what's the point of studying DS&A if you're just using helper methods? Also, you can just Google that question, it's the most basic question ever.
@lakshyamathur8808
@lakshyamathur8808 8 ай бұрын
@@zfarahx ok bro 🙏♥️♥️
@mstinku9003
@mstinku9003 4 ай бұрын
class Solution: def canConstruct(self, r: str, m: str) -> bool: k=Counter(r) l=Counter(m) return k
@mstinku9003
@mstinku9003 4 ай бұрын
U CAN LITERALLY COMPARE COUNTERS DIRECTLY from collections import Counter counter1 = Counter([1, 2, 3]) counter2 = Counter([1, 2, 2, 3, 3, 3]) print(counter1
@hlubradio2318
@hlubradio2318 7 ай бұрын
Very nice as always
@GregHogg
@GregHogg 7 ай бұрын
Glad to hear it!
@qulinxao
@qulinxao 6 ай бұрын
return not(Counter(ransomeNote)-Counter(magazin))
@venzdrop
@venzdrop 4 ай бұрын
1 liners fr lmao. yall crazy. it worked.
@brianwkinyua
@brianwkinyua 4 ай бұрын
wow! 👍
@khalidhussien6764
@khalidhussien6764 3 ай бұрын
standard array is a little quicker since the input is lower case english letters. int arr[26]; int pos = 0; for(char letter : magazine) { pos = letter - 97; arr[pos]++; } for(char letter : ransomNote) { pos = letter - 97; if(arr[pos] != 0) arr[pos]--; else return false; } return true;
Majority Element - Leetcode 169 - Hashmaps & Sets (Python)
8:00
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 341 М.
So Cute 🥰 who is better?
00:15
dednahype
Рет қаралды 19 МЛН
To Brawl AND BEYOND!
00:51
Brawl Stars
Рет қаралды 17 МЛН
When you have a very capricious child 😂😘👍
00:16
Like Asiya
Рет қаралды 18 МЛН
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 861 М.
8 Data Structures Every Programmer Should Know
17:09
ForrestKnight
Рет қаралды 243 М.
Ransom Note   Leetcode 383 Python
9:39
Persistent Programmer
Рет қаралды 4,9 М.
Stacks & Queues - DSA Course in Python Lecture 5
14:58
Greg Hogg
Рет қаралды 16 М.
Learn Machine Learning Like a GENIUS and Not Waste Time
15:03
Infinite Codes
Рет қаралды 373 М.
ML Was Hard Until I Learned These 5 Secrets!
13:11
Boris Meinardus
Рет қаралды 355 М.
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 316 М.
How to Start LeetCode from ZERO in 2025
11:31
Ashish Pratap Singh
Рет қаралды 86 М.