Problem Solving/LeetCode

Today's Challenge

fabichoi 2023. 1. 1. 23:45

https://leetcode.com/problems/word-pattern/

 

Word Pattern - LeetCode

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 ca

leetcode.com

너무 쉬운 문제일줄 알았는데, 결국 한시간 넘게 WA 난무하다가 다른 사람 솔루션 보고 제출.
1:1 매칭이라는 점을 이용해서 set(a), set(b), set(zip_longest(a,b)) 를 해서 비교하면 되는 문제였음.

zip_longest(a, b) 의 경우 a,b를 zipping 하되, 길이가 다른 것도 할 수 있게 만들어진 함수(매칭 할 값이 없으면 None)

현업에서 쓸만한 함수를 하나 건진 듯..?

class Solution:
    def wordPattern(self, pattern: str, s: str) -> bool:
        s = s.split()
        return (len(set(pattern)) == len(set(s)) == len(set(zip_longest(pattern, s))))
반응형