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 |
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 2395. Find Subarrays With Equal Sum (0) | 2023.03.13 |
---|---|
백준 27866 문자와 문자열 (0) | 2023.03.13 |
leetcode 2264. Largest 3-Same-Digit Number in String (0) | 2023.03.07 |
leetcode 2549. Count Distinct Numbers on Board (0) | 2023.03.07 |
leetcode 1539. Kth Missing Positive Number (0) | 2023.03.06 |
댓글