알고리즘
[해커랭크]Hash Tables: Ransom Note
띵유
2021. 5. 6. 17:00
반응형
첫번째 줄 : magazine
두번째 줄 : note
print Yes if he can replicate his ransom note exactly using whole words from the magazine; otherwise, print No.
def checkMagazine(magazine, note):
dict_mag={}
for word in magazine:
if word not in dict_mag:
dict_mag[word]=1
else:
dict_mag[word]+=1
for word in note:
if word not in dict_mag or dict_mag[word]==0:
print("No")
return ;
if word in dict_mag :
dict_mag[word]-=1
print("Yes")
return;
문제 교훈!!
값 비교시에는 비교하는 시점에 주의하자!
증감 연산자 사용 후 비교? 증감 전 비교??주의!
반응형