Find the Difference --LeetCode

来源:互联网 发布:天猫魔盒网络无法连接 编辑:程序博客网 时间:2024/05/18 13:05

Find the Difference
Difficulty: Easy

Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one
more letter at a random position.

Find the letter that was added in t.

Example:

> Input: s = "abcd" t = "abcde"> > Output: e

Explanation:
‘e’ is the letter that was added.

用到collection模块的Counter: 计数器,主要用来计数

实现:

class Solution(object):    def findTheDifference(self, s, t):        """        :type s: str        :type t: str        :rtype: str        """        ds = collections.Counter(s)        dt = collections.Counter(t)        return (dt - ds).keys().pop() #*counter返回一个字典,dt减去ds去掉重合的部分字符*
0 0
原创粉丝点击