일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 잡생각
- Daily Challenge
- 쓰릴오브파이트
- 리얼 클래스
- Problem Solving
- 10분
- 파비최
- 월간
- FIT XR
- Writing
- English
- 스탭퍼
- 영어원서읽기
- 괜찮음
- 30분
- 영어공부
- 읽기
- realclass
- 개발자
- 3줄정리
- 미드시청
- 링피트
- 매일
- 프로젝트
- leetcode
- 만화도
- 사이드
- 뭐든
- 화상영어
- 운동
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