| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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줄정리
- 영어원서읽기
- 파비최
- 잡생각
- English
- 월간
- 10분
- 괜찮음
- Problem Solving
- Writing
- Daily Challenge
- 뭐든
- 쓰릴오브파이트
- realclass
- 사이드
- leetcode
- 읽기
- FIT XR
- 미드시청
- 스탭퍼
- 만화도
- 리얼 클래스
- 운동
- 30분
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.04.16 Today's Challenge 본문
https://leetcode.com/problems/number-of-ways-to-form-a-target-string-given-a-dictionary/
Number of Ways to Form a Target String Given a Dictionary - LeetCode
Can you solve this real interview question? Number of Ways to Form a Target String Given a Dictionary - You are given a list of strings of the same length words and a string target. Your task is to form target using the given words under the following rule
leetcode.com
class Solution:
def numWays(self, words: List[str], target: str) -> int:
ab = 26
mod = 1000000007
m, k = len(target), len(words[0])
cnt = [[0] * k for _ in range(ab)]
for j in range(k):
for word in words:
cnt[ord(word[j]) - ord('a')][j] += 1
dp = [[0] * (k+1) for _ in range(m+1)]
dp[0][0] = 1
for i in range(m+1):
for j in range(k):
if i < m:
dp[i+1][j+1] += (cnt[ord(target[i]) - ord('a')][j] * dp[i][j])
dp[i+1][j+1] %= mod
dp[i][j+1] += dp[i][j]
dp[i][j+1] %= mod
return dp[m][k]반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
| 2023.04.18 Today's Challenge (0) | 2023.04.18 |
|---|---|
| 2023.04.17 Today's Challenge (0) | 2023.04.17 |
| 2023.04.15 Today's Challenge (0) | 2023.04.15 |
| 2023.04.14 Today's Challenge (0) | 2023.04.14 |
| 2023.04.13 Today's Challenge (0) | 2023.04.13 |
Comments