Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- SW Expert Academy
- 자연어처리
- Real or Not? NLP with Disaster Tweets
- 프로그래머스
- Git
- Docker
- github
- 금융문자분석경진대회
- 편스토랑 우승상품
- Kaggle
- 캐치카페
- 우분투
- Baekjoon
- 파이썬
- 데이콘
- 편스토랑
- ChatGPT
- 프로그래머스 파이썬
- hackerrank
- leetcode
- 백준
- gs25
- 코로나19
- 맥북
- AI 경진대회
- PYTHON
- 더현대서울 맛집
- programmers
- ubuntu
- dacon
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 290. Word Pattern (Python) 본문
728x90
반응형
Given a pattern and a string str, find if str 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 str.
Example 1:
Input: pattern = "abba", str = "dog cat cat dog"
Output: true
Example 2:
Input:pattern = "abba", str = "dog cat cat fish"
Output: false
Example 3:
Input: pattern = "aaaa", str = "dog cat cat dog"
Output: false
Example 4:
Input: pattern = "abba", str = "dog dog dog dog"
Output: false
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters that may be separated by a single space.
Solution
class Solution:
def wordPattern(self, pattern: str, string: str) -> bool:
string_s = string.split(' ')
answer = True
check_dict = {}
for p, s in zip(pattern, string_s):
if p not in check_dict.keys():
check_dict[p] = s
else:
if check_dict[p] != s:
answer = False
break
if (len(set(check_dict.keys())) != len(set(check_dict.values()))) or (len(pattern) != len(string_s)):
answer = False
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 448. Find All Numbers Disappeared in an Array (Python) (0) | 2020.08.06 |
---|---|
[leetCode] 35. Search Insert Position (Python) (0) | 2020.08.05 |
[leetCode] 205. Isomorphic Strings (Python) (0) | 2020.08.03 |
[leetCode] 434. Number of Segments in a String (Python) (0) | 2020.08.02 |
[leetCode] 1528. Shuffle String (Python) (0) | 2020.08.02 |
Comments