엔지니어 게시판
LeetCode 솔루션 분류

[1/1] 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.

 

Example 1:

Input: pattern = "abba", s = "dog cat cat dog"
Output: true

Example 2:

Input: pattern = "abba", s = "dog cat cat fish"
Output: false

Example 3:

Input: pattern = "aaaa", s = "dog cat cat dog"
Output: false

 

Constraints:

  • 1 <= pattern.length <= 300
  • pattern contains only lower-case English letters.
  • 1 <= s.length <= 3000
  • s contains only lowercase English letters and spaces ' '.<li style="border: 0px solid; box-sizing: border-box; --tw-border-spacing-x:0; --tw-border-spacing-y:0; --tw-translate-x:0; --tw-translate-y:0; --tw-rotate:0; --tw-skew-x:0; --tw-skew-y:0; --tw-scale-x:1; --tw-scale-y:1; --tw-pan-x: ; --tw-pan-y: ; --tw-pinch-zoom: ; --tw-scroll-snap-strictness:proximity; --tw-ordinal: ; --tw-slashed-zero: ;

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
class Solution:
    def wordPattern(self, pattern: str, s: str) -> bool:
        pat_to_word = {}
        slist = s.split()

        word_to_pat = {}

        if len(pattern) != len(slist): return False
        for i in range(len(slist)):
            if pattern[i] not in pat_to_word:
                pat_to_word[pattern[i]] = slist[i]
            elif pat_to_word[pattern[i]] != slist[i]:
                return False
            
            if slist[i] not in word_to_pat:
                word_to_pat[slist[i]] = pattern[i]
            elif word_to_pat[slist[i]] != pattern[i]:
                return False
        
        return True
전체 396 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 158 명
  • 오늘 방문자 2,300 명
  • 어제 방문자 6,462 명
  • 최대 방문자 11,134 명
  • 전체 회원수 1,050 명
알림 0