Find the Difference
来源:互联网 发布:星际公民 知乎 编辑:程序博客网 时间:2024/05/18 01:51
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.1-liners and 2-liner in PythonUsing XOR:class Solution(object): def findTheDifference(self, s, t): return chr(reduce(operator.xor, map(ord, s + t)))Using collections.Counter():class Solution(object): def findTheDifference(self, s, t): return list((collections.Counter(t) - collections.Counter(s)))[0]A 2-liner here using sorted():class Solution(object): def findTheDifference(self, s, t): s, t = sorted(s), sorted(t) return t[-1] if s == t[:-1] else [x[1] for x in zip(s, t) if x[0] != x[1]][0]
0 0
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- Find the Difference
- 160.Find the Difference
- LeetCode389. Find the Difference
- Find the Difference
- 389. Find the Difference
- Leetcode:Find the Difference
- leetcode_389 Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- Find the Difference --LeetCode
- [389]Find the Difference
- 389. Find the Difference
- LeetCode Find the Difference
- Find the Difference
- linux串口编程参数配置详解
- SVN Files 的值“ < < < < < < < .mine”无效。路径中具有非法字符。
- 【转】java对音频文件的频谱分析
- tcp协议完美博客网址
- QML 和QtQuick2
- Find the Difference
- Java IO流学习总结
- 前端性能优化----yahoo前端性能团队总结的35条黄金定律 20
- Spring事务管理——慕课网
- java--06--多线程(一)
- 正向代理与反向代理的区别
- 数据结构与算法分析笔记与总结(java实现)--二叉树笔记
- ubuntu 搭建git仓库
- textfield,首字符不能输入“0”或者“.”,控制小数点后面的分位数以及不让小数点出现两次