Word Pattern - LeetCode
Can you solve this real interview question? Word Pattern - Given a pattern and a string s, find if s follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s. Example
leetcode.com
전체 코드
class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
if len(pattern) != len(s.split()):
return False
p_dict = {}
i = 1
for p in pattern:
if p_dict.get(p, 0) == 0:
p_dict[p] = i
i += 1
pat = ""
for p in pattern:
pat += str(p_dict[p])
s_dict = {}
i = 1
for ss in s.split():
if s_dict.get(ss, 0) == 0:
s_dict[ss] = i
i += 1
sat = ""
for ss in s.split():
sat += str(s_dict[ss])
return True if pat == sat else False
숫자로 대치해서 비교했습니다 :>
'Coding Test > Python' 카테고리의 다른 글
[프로그래머스] 이중우선순위큐 (level3, python) (0) | 2024.01.16 |
---|---|
[프로그래머스] 디스크 컨트롤러 (level3, python) (1) | 2024.01.16 |
[LeetCode] 383. Ransom Note (easy, python) (0) | 2024.01.15 |
[프로그래머스] 더 맵게 (level2, python) (1) | 2024.01.13 |
[프로그래머스] 프로세스 (level2, python) (0) | 2024.01.11 |