일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- github
- 데이콘
- leetcode
- Real or Not? NLP with Disaster Tweets
- 프로그래머스 파이썬
- 캐치카페
- 맥북
- hackerrank
- gs25
- dacon
- Git
- 우분투
- 금융문자분석경진대회
- ubuntu
- 파이썬
- ChatGPT
- SW Expert Academy
- programmers
- 편스토랑 우승상품
- Docker
- 자연어처리
- 더현대서울 맛집
- 편스토랑
- Baekjoon
- AI 경진대회
- 백준
- 프로그래머스
- Kaggle
- PYTHON
- 코로나19
- Today
- Total
솜씨좋은장씨
[HackerRank] Hash Tables : Ransom Note (Python) 본문
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 [ i ].
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 하도록함.
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 929. Unique Email Addresses (Python) (0) | 2020.03.24 |
---|---|
[BaeKJoon] 10844번: 쉬운 계단수 (Python) (0) | 2020.03.23 |
[SW_Expert_Academy] 4581번 문자열 재구성 프로젝트 (Python) (0) | 2020.03.21 |
[HackerRank] Stacks and Queues : Balanced Brackets (Python) (0) | 2020.03.20 |
[BaeKJoon] 11726번: 2xn타일링 (Python) (0) | 2020.03.19 |