290. Word Pattern

Doyeon Kim·2022년 7월 3일

코딩테스트 공부

목록 보기
90/171

문제 링크 : https://leetcode.com/problems/word-pattern/


이전에 풀었던 Isomorphic Strings와 매우 흡사한 문제이다.

그래서 이정네 풀었던 것을 이용하여 풀었는데 왜인지 계속 틀렸다고 뜨길래 왜인지 봤더니 알고보니
.split()해줘야함

split()은 whitespace를 기준으로 문자열을 나눕니다.

text = 'Hello world, python'
strings = text.split()
print(strings)

Output:

['Hello', 'world,', 'python']

그래서 다시 수정해서 풀어봄
1차시도

class Solution:
    def wordPattern(self, pattern: str, s: str) -> bool:
        sobad = s.split()
        zipped = set(zip(pattern, sobad))
        return len(zipped) == len(set(pattern)) == len(set(sobad))

실패!

실패 테.케
Input
"aba"
"cat cat cat dog"
Output
true
Expected
false

그래서 추가로 조건을 더 달아주고 보완하여 제출하였다


class Solution:
    def wordPattern(self, pattern: str, s: str) -> bool:
        sobad = s.split()
        return len(set(zip(pattern, sobad))) == len(set(pattern)) == len(set(sobad)) and len(pattern) == len(sobad)

성공!

profile
성장하고 도전하는 개발자. 프로그래밍 좋아하세요?

0개의 댓글