본문 바로가기
python-algorithm

leetcode 2451. Odd String Difference

by 무적김두칠 2023. 3. 8.

https://leetcode.com/problems/odd-string-difference/description/

 

Odd String Difference - LeetCode

Can you solve this real interview question? Odd String Difference - You are given an array of equal-length strings words. Assume that the length of each string is n. Each string words[i] can be converted into a difference integer array difference[i] of len

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
    def oddString(self, words: List[str]) -> str:
        #calculate difference among input string
        def cal_difference(s: str-> list[int]:
            difference_array = []
            for i in range(1,len(s)):
                #ord('a') equals 97 
                difference_array.append(ord(s[i])-97 - (ord(s[i-1])-97) )
            return difference_array
        list_difference = []
        for word in words:
            list_difference.append(cal_difference(word))
        
        for sublist in list_difference:
            # count() == 1 means , This sublist is only one value among other values
            if list_difference.count(sublist) == 1:
                answer = words[list_difference.index(sublist)]
                return answer
cs
반응형

댓글