728x90
class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
s = list(s.split(" "))
if len(pattern) != len(s):
return False
s2t, t2s = {}, {}
for p, w in zip(pattern, s):
if w not in s2t and p not in t2s:
s2t[w] = p
t2s[p] = w
elif w not in s2t or s2t[w] != p:
# Contradict mapping.
return False
return True
이 문제를 본 순간
[LeetCode] Isomorphic Strings 문제가 떠올랐다. ygseo.tistory.com/221
len으로 먼저 length를 체크해줘야 하는 case가 있어서 오류가 났었다.
따라서 len 체크부터 하고 나머지는
그냥 isomorphic string 문제와 똑같이.
728x90
'Leetcode' 카테고리의 다른 글
[LeetCode] Reverse Vowels of String 파이썬 (swap, two pointer) (0) | 2021.04.13 |
---|---|
[LeetCode] Reverse String 파이썬 (in-place, list.reverse()) (0) | 2021.04.12 |
[LeetCode] Move Zeros 파이썬 (in-place) (0) | 2021.04.12 |
[LeetCode] Missing Number 파이썬 (0) | 2021.04.12 |
[LeetCode] Ugly Number 파이썬 (0) | 2021.04.11 |
댓글