博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode389. Find the Difference
阅读量:2243 次
发布时间:2019-05-09

本文共 944 字,大约阅读时间需要 3 分钟。

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:eExplanation:'e' is the letter that was added.

思路:比较两个字符串中新添加的字符(字符顺序可能不同)。最开始想法是利用两个字符串的Counters直接相减,返回不同:

from collections import Counterclass Solution:    def findTheDifference(self, s, t):        """        :type s: str        :type t: str        :rtype: str        """        dic1 = Counter(s)        dic2 = Counter(t)                return(list(dic2-dic1)[0])

又想到两个字符串除了一个字符不同,剩下全都一致,那么两个字符串加起来,只有这一个多添加的字符个数为奇数。方法有很多。

from collections import Counterclass Solution:    def findTheDifference(self, s, t):        """        :type s: str        :type t: str        :rtype: str        """        dic = Counter(s+t)        for k,v in dic.items():            if v%2 !=0 :                return k

 

转载地址:http://pprbb.baihongyu.com/

你可能感兴趣的文章
【Python】详解Python多线程Selenium跨浏览器测试
查看>>
Jmeter之参数化
查看>>
Shell 和Python的区别。
查看>>
【JMeter】1.9上考试jmeter测试调试
查看>>
【虫师】【selenium】参数化
查看>>
【Python练习】文件引用用户名密码登录系统
查看>>
学习网站汇总
查看>>
【Loadrunner】性能测试报告实战
查看>>
【自动化测试】自动化测试需要了解的的一些事情。
查看>>
【selenium】selenium ide的安装过程
查看>>
【手机自动化测试】monkey测试
查看>>
【英语】软件开发常用英语词汇
查看>>
Fiddler 抓包工具总结
查看>>
【雅思】雅思需要购买和准备的学习资料
查看>>
【雅思】雅思写作作业(1)
查看>>
【雅思】【大作文】【审题作业】关于同不同意的审题作业(重点)
查看>>
【Loadrunner】通过loadrunner录制时候有事件但是白页无法出来登录页怎么办?
查看>>
【English】【托业】【四六级】写译高频词汇
查看>>
【托业】【新东方全真模拟】01~02-----P5~6
查看>>
【托业】【新东方全真模拟】03~04-----P5~6
查看>>