파비의 매일매일 공부기록

2023.02.02 Today's Challenge 본문

Problem Solving/LeetCode

2023.02.02 Today's Challenge

fabichoi 2023. 2. 2. 23:45

https://leetcode.com/problems/verifying-an-alien-dictionary/

 

Verifying an Alien Dictionary - LeetCode

Verifying an Alien Dictionary - In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters. Given a sequence of words written in the ali

leetcode.com

단순히 한글자 비교로는 이슈가 생김.
Map으로 구현하면 됨

class Solution:
    def isAlienSorted(self, words: List[str], order: str) -> bool:
        map = {}
        for i in range(len(order)):
            map[order[i]] = i
        for i in range(1, len(words)):
            first = words[i-1]
            second = words[i]
            n = min(len(first), len(second))
            flag = False
            for j in range(n):
                if map[first[j]] < map[second[j]]:
                    flag = True
                    break
                elif map[first[j]] > map[second[j]]:
                    return False
            if not flag and len(first) > len(second):
                return False
        return True
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.02.04 Today's Challenge  (0) 2023.02.04
2023.02.03 Today's Challenge  (0) 2023.02.03
2023.02.01 Today's Challenge  (0) 2023.02.01
2023.01.31 Today's Challenge  (0) 2023.01.31
2023.01.30 Today's Challenge  (1) 2023.01.30
Comments