관리 메뉴

솜씨좋은장씨

[HackerRank] Hash Tables : Ransom Note (Python) 본문

Programming/코딩 1일 1문제

[HackerRank] Hash Tables : Ransom Note (Python)

솜씨좋은장씨 2020. 3. 22. 03:50
728x90
반응형

Harold is a kidnapper who wrote a ransom note, but now he is worried it will be traced back to him through his handwriting. He found a magazine and wants to know if he can cut out whole words from it and use them to create an untraceable replica of his ransom note. The words in his note are case-sensitive and he must use only whole words available in the magazine. He cannot use substrings or concatenation to create the words he needs.

Given the words in the magazine and the words in the ransom note, print Yes if he can replicate his ransom note exactly using whole words from the magazine; otherwise, print No.

For example, the note is "Attack at dawn". The magazine contains only "attack at dawn". The magazine has all the right words, but there's a case mismatch. The answer is No.

 

Function Description

Complete the checkMagazine function in the editor below. It must print Yes if the note can be formed using the magazine, or No.

checkMagazine has the following parameters:

  • magazine: an array of strings, each a word in the magazine
  • note: an array of strings, each a word in the ransom note

Input Format

The first line contains two space-separated integers,  and , the numbers of words in the magazine and the note..
The second line contains m space-separated strings, each magazine [ ].
The third line contains n space-separated strings, each note [ i  ].

 

Constraints

  • 1 <= m, n <= 30000
  • .1 <= | magazine [ i  ] |, | note [ i  ] | <= 5
  • Each word consists of English alphabetic letters (i.e., a to z and A to Z ).

Output Format

Print Yes if he can use the magazine to create an untraceable replica of his ransom note. Otherwise, print No.

 

Sample Input 0

6 4
give me one grand today night
give one grand today

Sample Output 0

Yes

Sample Input 1

6 5
two times three is not four
two times two is four

Sample Output 1

No

Explanation 1

'two' only occurs once in the magazine.

 

Sample Input 2

7 4
ive got a lovely bunch of coconuts
ive got some coconuts

Sample Output 2

No

Explanation 2

Harold's magazine is missing the word some.

 

Solution

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the checkMagazine function below.
def checkMagazine(magazine, note):


    answer = 'Yes'

    magazine_dic = {}

    for i in range(len(magazine)):
        if magazine[i] not in magazine_dic.keys():
            magazine_dic[magazine[i]] = 1
        else:
            magazine_dic[magazine[i]] = magazine_dic[magazine[i]] + 1
    # print(magazine_dic)
    for i in range(len(note)):
        if note[i] not in magazine_dic.keys():
            answer = 'No'
            break
        else:
            magazine_dic[note[i]] = magazine_dic[note[i]] - 1

            if magazine_dic[note[i]] < 0:
                answer = "No"
                break 
    return answer

Solution 풀이

먼저 magazine에 있는 단어리스트를 { 단어 : 빈도수 } 형태의 dictionary로 만들어줍니다.

두번째 note에 있는 단어리스트를 반복문으로 돌면서

앞에서 만든 dictionary의 key값에 없는 단어가 존재하면 반복문을 중단하고 "No"를 return하도록하고

그렇지 않을 경우 해당 단어값을 키값으로 dictionary에 접근하여 해당 값에 -1을 해주고

만약 그 값이 0보다 작아질 경우 "No"를 return 하도록함.

 

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

 

Comments