일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 파비최
- 3줄정리
- 운동
- 개발자
- Problem Solving
- Daily Challenge
- 읽기
- Writing
- 잡생각
- 화상영어
- 30분
- English
- 10분
- 미드시청
- 프로젝트
- 링피트
- 뭐든
- 만화도
- 괜찮음
- 쓰릴오브파이트
- 리얼 클래스
- 스탭퍼
- 월간
- FIT XR
- 영어원서읽기
- realclass
- leetcode
- 사이드
- 영어공부
- 매일
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.12.05 Today's Challenge 본문
Find Words That Can Be Formed by Characters - LeetCode
Can you solve this real interview question? Find Words That Can Be Formed by Characters - You are given an array of strings words and a string chars. A string is good if it can be formed by characters from chars (each character can only be used once). Retu
leetcode.com
해쉬맵 형태로 푸는 법 말고
좀 더 쉬운 방법은 없을까..
class Solution:
def countCharacters(self, words: List[str], chars: str) -> int:
cnt = [0] * 26
for c in chars:
cnt[ord(c) - ord('a')] += 1
ans = 0
for word in words:
word_cnt = [0] * 26
for c in word:
word_cnt[ord(c) - ord('a')] += 1
is_ok = True
for i in range(26):
if cnt[i] < word_cnt[i]:
is_ok = False
break
if is_ok:
ans += len(word)
return ans
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.12.07 Today's Challenge (0) | 2023.12.07 |
---|---|
2023.12.06 Today's Challenge (0) | 2023.12.06 |
2023.12.04 Today's Challenge (1) | 2023.12.04 |
2023.12.03 Today's Challenge (0) | 2023.12.03 |
2023.12.02 Today's Challenge (1) | 2023.12.02 |
Comments