Can you solve this Easy Google Interview Question? Ransom Note - Leetcode 383

preview_player
Показать описание
Can you solve this Easy Google Interview Question? Ransom Note - Leetcode 383
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms for FREE at AlgoMap.io!

GregHogg
Автор

This question was asked for the position of the janitor.

You can just use a integer frequency array. Increase the frequency of character from string 1 and decrease the frequency of characters from string 2 and check if the array has any non-zero elements left.

ShinAkuma
Автор

A way faster approach would be to build frequency arrays on both and make sure each element in the note is smaller than the one in the magazine

llumnate
Автор

I like your shorts because they show how ignorant am I and cool stuff I could learn. I only do simple coding for work but like the theory pretty much.

TheOmegaAlfa
Автор

Can we just sort both of them and compare them

ZaeemMZia
Автор

How about a bucket sort, have an array of how many a's, b's and so forth. Then you see if you have more or equal in the mag array than the note array for each letter. It is false if any letter has more in the note array. This is also an O(n) solution.

BovineFreedo
Автор

Whats the difference between hashmap and dictionary i built in python?

nihalbhamrah
Автор

I tried a different approch

def check (st1, st2):
st1= st1.lower();
st2= st2.lower();
list =[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for char in st1:
index=ord(char)-ord('a')
list[index]+=1
for char in st2:
index=ord(char)-ord('a')
list[index]-=1

for x in list:
if x!=0:
return False
return True


st1="abc"
st2="cba"
print(check(st1, st2))


st1="aaabd"
st2="bdaa"
print(check(st1, st2))

abdulsaboor
Автор

how do you do something like the minimum number of magazine "strips" where strips is a contiguous substring?

troys
Автор

Use a frequency array with each letter being represented by its ASCII value subtracted by the ASCII value of 'a'. Do this for both the Note and the magazine and then loop through each one. That is way better than using a dictoonary, even though both solutions are technically O(n).

guilhermewxs
Автор

Two frequency counter maps. Then loop the ransom map: if ransom key not in magazine map or ransom counter value > magazine counter value: return False

rdubb
Автор

Greg with another banger for noob coders like me

obgbbyn
Автор

Brute force would be sort(a), sort(b) and return a==b

saikiranmajji
Автор

We don't need to decrement the counter.
Just store elements of ransomnote & magazine in map.
Now for every element of ransomnote, make sure that the element u r checking in ransomnote, the frequency is greater or equal to in map of magazine than in ransomnote

arinkulkarni
Автор

What’s the point.

Going to get my Elevator Certification

paulsingh
Автор

Sort both. Iterate and return false if a char doesnt match. O(n)

MadChrisp
Автор

Lol the butt hurted people in the comments

architmishra
Автор

I'd rather put my efforts into running my own business (tech) than join Google and eventually join their graveyard. This isn't 2001, Google lost its shine long ago.

BloodyIron
visit shbcf.ru