-
Notifications
You must be signed in to change notification settings - Fork 0
/
389-找不同.py
68 lines (52 loc) · 1.37 KB
/
389-找不同.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- encoding: utf-8 -*-
'''
@File : 389-找不同.py
@Time : 2021/06/08 15:05:27
@Author : TYUT ltf
@Version : v1.0
@Contact : 18235121656@163.com
@License : (C)Copyright 2020-2030, GNU General Public License
'''
# here put the import lib
import collections
'''
给定两个字符串 s 和 t,它们只包含小写字母。
字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
请找出在 t 中被添加的字母。
示例 1:
输入:s = "abcd", t = "abcde"
输出:"e"
解释:'e' 是那个被添加的字母。
示例 2:
输入:s = "", t = "y"
输出:"y"
示例 3:
输入:s = "a", t = "aa"
输出:"a"
示例 4:
输入:s = "ae", t = "aea"
输出:"a"
提示:
0 <= s.length <= 1000
t.length == s.length + 1
s 和 t 只包含小写字母
'''
class Solution:
def findTheDifference(self, s: str, t: str):
# 俩循环 列表
# a = list(s)
# b = list(t)
# for i in a:
# if i in b:
# b.remove(i)
# return b[0]
# 统计字符串每个字符的字数 不相等的即是目标元素
countS = collections.Counter(s)
countT = collections.Counter(t)
for val in countT:
if countT[val] != countS[val]:
return val
obj = Solution()
print(obj.findTheDifference('abcd', 'abcde'))