难度:简单
给你一个字符串数组?
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
中?差值整数数组?不同的字符串。示例 1:
输入:words = ["adc","wzy","abc"] 输出:"abc" 解释: - "adc" 的差值整数数组是 [3 - 0, 2 - 3] = [3, -1] 。 - "wzy" 的差值整数数组是 [25 - 22, 24 - 25]= [3, -1] 。 - "abc" 的差值整数数组是 [1 - 0, 2 - 1] = [1, 1] 。 不同的数组是 [1, 1],所以返回对应的字符串,"abc"。示例 2:
输入:words = ["aaa","bob","ccc","ddd"] 输出:"bob" 解释:除了 "bob" 的差值整数数组是 [13, -13] 以外,其他字符串的差值整数数组都是 [0, 0] 。提示:
3 <= words.length <= 100
n == words[i].length
2 <= n <= 20
words[i]
?只含有小写英文字母。题解:
class Solution(object): def oddString(self, words): word_dict = { 'a':0,'b':1,'c':2,'d':3,'e':4,'f':5,'g':6,'h':7,'i':8,'j':9,'k':10, 'l':11,'m':12,'n':13,'o':14,'p':15,'q':16,'r':17,'s':18,'t':19,'u':20, 'v':21,'w':22,'x':23,'y':24,'z':25 } res = [] for i in words: s = [] for j in range(1,len(i)): r = word_dict[i[j]] - word_dict[i[j-1]] # print(r) s.append(r) res.append(s) c = 0 for m in res: c = res.count(m) if c == 1: return words[res.index(m)]