728x90
▶290 - 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.
▶예제
Input: pattern = "abba", s = "dog cat cat dog"
Output: true
Input: pattern = "abba", s = "dog cat cat fish"
Output: false
Input: pattern = "aaaa", s = "dog cat cat dog"
Output: false
▶풀이
class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
arr = s.split(' ')
if len(pattern) != len(arr):
return False
pdic = {}
sdic = {}
for i in range(len(pattern)):
p = pattern[i]
a = arr[i]
if (p in pdic and pdic[p] != a) or (a in sdic and sdic[a] != p):
return False
pdic[p] = a
sdic[a] = p
return True
728x90
'LeetCode' 카테고리의 다른 글
[LeetCode] Medium - 55 Jump Game (0) | 2023.01.04 |
---|---|
[LeetCode] Easy - 520 Detect Capital (0) | 2023.01.02 |
[LeetCode] Hard - 980 Unique Paths III (1) | 2022.12.31 |
[LeetCode] Medium - 309 Best Time to Buy and Sell Stock with Cooldown (1) | 2022.12.23 |
[LeetCode] Medium - 886 Possible Bipartition (0) | 2022.12.21 |