Easy Google Interview Question! Ransom Note - Leetcode 383

preview_player
Показать описание
Easy Google Interview Question! Ransom Note - Leetcode 383

#coding #leetcode #programming #interview
Рекомендации по теме
Комментарии
Автор

Happy that I could recognize this as a hashmap problem at a glance

sorbet-fox
Автор

Half of the coding questions can be solved with a hashmap and I was wondering for LIKE HALF A YEAR WHAT A HASH MAP WAS (I knew structures (game engine based on js) and dictionaries in python 😭)

NiQsterVX
Автор

53 days in self studying python but i only understood 2/3rds in my own puesdo code 😭 your shorts really are helpful!!

luffynumberluffynumber
Автор

Thank you for creating and sharing this! I am sure your course is a great value!

runsurfswim
Автор

Love it. Can u add the link for the question. I think doing that for all questions will be a idea in case some one to follow up the question

fuseinimohammed
Автор

One question: why do you always choose from the easy problems set?

axixur
Автор

Yeah there is another way with charCodeAt method of string where you push the characters' numbers and then you minus them in second loop then return every a>=0 if there is negative it will return false meaning the letter that does not exist in array was subtracted

zoirjohn
Автор

return not Counter(ransomNote) - Counter(magazine)

alhassanelkossei
Автор

This is better:

class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
if len(ransomNote) > len(magazine):
return False

mgz = {}
for x in magazine:
mgz[x] = mgz.get(x, 0) + 1

for x in ransomNote:
if mgz.get(x, 0) == 0:
return False
else:
mgz[x] -= 1
return True

This is shorter:

class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
if len(ransomNote) > len(magazine):
return False

for x in ransomNote:
if x in magazine:
magazine = magazine.replace(x, "", 1)
else:
return False

return True

giorgizazadze
Автор

Maybe I'm thinking wrong but wouldn't it be slightly more efficient to create a hashmap for the result, like the ransom string? Like a smaller lookup to save up on memory and ram bus performance?

Edit: Also removing shouldn't be necessary, just consumes cpu cycles if we dont just instead loop over magazine letters once

skylo
Автор

I thought I can sort both the strings and check each character

rohithchandrathonupunoori
Автор

in JS just make the new set from the magazine string
and compare

HarishV-uxks
Автор

Me: easy with a hash map.. but maybe there’s a more elegant way to solve it. After 5 minutes of thinking with the video on pause, gave up and played. Then hash map 😣

victors
Автор

why the- why would you use a hashmap??
you need like 4 cachelines worth of memory for this. just use an array...

DemonixTB
Автор

So a hashmap is just the implementation of a dictionary 🙈

urboss
Автор

What real life application does this has ? If it had any ... 🙄
Before solving crap we should ask this, if the answer is "no IRL applications" then pass

MrDragos
visit shbcf.ru