Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 편스토랑
- 백준
- 우분투
- 프로그래머스
- github
- Docker
- 파이썬
- 금융문자분석경진대회
- 편스토랑 우승상품
- Real or Not? NLP with Disaster Tweets
- Git
- ubuntu
- 자연어처리
- 맥북
- Kaggle
- programmers
- 더현대서울 맛집
- ChatGPT
- PYTHON
- leetcode
- 데이콘
- Baekjoon
- 코로나19
- SW Expert Academy
- dacon
- 캐치카페
- AI 경진대회
- hackerrank
- 프로그래머스 파이썬
- gs25
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 916. Word Subsets (Python) 본문
728x90
반응형
We are given two arrays A and B of words. Each word is a string of lowercase letters.
Now, say that word b is a subset of word a if every letter in b occurs in a, including multiplicity. For example, "wrr" is a subset of "warrior", but is not a subset of "world".
Now say a word a from A is universal if for every b in B, b is a subset of a.
Return a list of all universal words in A. You can return the words in any order.
Example 1:
Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["e","o"]
Output: ["facebook","google","leetcode"]
Example 2:
Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["l","e"]
Output: ["apple","google","leetcode"]
Example 3:
Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["e","oo"]
Output: ["facebook","google"]
Example 4:
Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["lo","eo"]
Output: ["google","leetcode"]
Example 5:
Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["ec","oc","ceo"]
Output: ["facebook","leetcode"]
Note:
- 1 <= A.length, B.length <= 10000
- 1 <= A[i].length, B[i].length <= 10
- A[i] and B[i] consist only of lowercase letters.
- All words in A[i] are unique: there isn't i != j with A[i] == A[j].
Solution
from collections import Counter
class Solution:
def wordSubsets(self, A: List[str], B: List[str]) -> List[str]:
answer=[]
B_cnt = {}
for b in B:
b_cnt=Counter(b)
for key in b_cnt:
B_cnt[key]=max(B_cnt.get(key,0),b_cnt[key])
for a in A:
a_cnt=Counter(a)
isCheck=True
for key in B_cnt:
if a_cnt.get(key,0)<B_cnt[key]:
isCheck=False
break
if isCheck == True:
answer.append(a)
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 121. Best Time to Buy and Sell Stock (Python) (0) | 2020.08.13 |
---|---|
[leetCode] 392. Is Subsequence (Python) (0) | 2020.08.13 |
[leetCode] 551. Student Attendance Record I (Python) (0) | 2020.08.08 |
[leetCode] 345. Reverse Vowels of a String (Python) (0) | 2020.08.07 |
[leetCode] 448. Find All Numbers Disappeared in an Array (Python) (0) | 2020.08.06 |
Comments