给你一个字符串数组 words
,每一个字符串长度都相同,令所有字符串的长度都为 n
。
每个字符串 words[i]
可以被转化为一个长度为 n - 1
的 差值整数数组 difference[i]
,其中对于 0 <= j <= n - 2
有 difference[i][j] = words[i][j+1] - words[i][j]
。注意两个字母的差值定义为它们在字母表中 位置 之差,也就是说 'a'
的位置是 0
,'b'
的位置是 1
,'z'
的位置是 25
。
- 比方说,字符串
"acb"
的差值整数数组是[2 - 0, 1 - 2] = [2, -1]
。
words
中所有字符串 除了一个字符串以外 ,其他字符串的差值整数数组都相同。你需要找到那个不同的字符串。
请你返回 words
中 差值整数数组 不同的字符串。
2451. 差值数组不同的字符串 – 力扣(Leetcode)

OpenCV步步精深-可心科创工作室
思路:
哈希表,然后找到一个的索引,根据索引找到对应的元素。
python3实现:
class Solution:
def oddString(self, words: List[str]) -> str:
# 哈希表
letter_low = [chr(elem) for elem in range(97,123) ]
hash_list = {}
for i in range(len(letter_low)):
hash_list[letter_low[i]] = i
result = []
for each_word in words:
tmp = []
p = 1
while p <= len(each_word) - 1:
tmp.append(hash_list[each_word[p]] - hash_list[each_word[p-1]])
p += 1
result.append(tmp)
print(result)
for thing in result:
if result.count(thing) == 1:
idx = result.index(thing)
return words[idx]

OpenCV步步精深-可心科创工作室