LeetCode——Find the Difference

Question

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.

Solution

  1. 用hash table求解,时间复杂度为O(2n)

  2. 用异或求解,这道题和single number 的问题是一个意思,两个字符串中,找出那个单个的字符。时间复杂度为O(n)。

Answer

class Solution {
public:char findTheDifference(string s, string t) {map<char, int> dict;for (char c : s)dict[c]++;map<char, int> dict2;for (char c : t) {dict2[c]++;if (dict2[c] > dict[c])return c;}}
};

用异或求解。

class Solution {
public:char findTheDifference(string s, string t) {char r=0;for(char c:s) r ^=c;for(char c:t) r ^=c;return r;}
};

转载于:https://www.cnblogs.com/zhonghuasong/p/6658700.html

LeetCode——Find the Difference相关推荐

  1. LeetCode Find the Difference

    题意:给出两个字符串 s,t,t是由s字符串打散组成,然后再添加个字符. 思路:因为字符串中可能有重复的字符,所以不能用set来做.用hashmap来做,遍历t字符串时,统计对应字符的个数,然后遍历字 ...

  2. C#LeetCode刷题之#530-二叉搜索树的最小绝对差(Minimum Absolute Difference in BST)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4123 访问. 给定一个所有节点为非负值的二叉搜索树,求树中任意两 ...

  3. LeetCode之Find the Difference

    1.题目 Given two strings s and t which consist of only lowercase letters. String t is generated by ran ...

  4. LeetCode 389. Find the Difference

    题目: Given two strings s and t which consist of only lowercase letters. String t is generated by rand ...

  5. C#LeetCode刷题之#389-找不同(Find the Difference)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4062 访问. 给定两个字符串 s 和 t,它们只包含小写字母. ...

  6. Leetcode 389. Find the Difference 找不同 解题报告

    1 解题思想 有两个字符串S和T,T是S打乱后多加一个字符,要找出这个不同的字符来. 嗯,这个问题其实就是异或,除了T多的那个字符外,都可以在异或中消除,不多说,很简单 2 原题 Given two ...

  7. 你面试稳了!通关LeetCode刷题完整攻略,省时又高效

    关注上方"深度学习技术前沿",选择"星标公众号", 资源干货,第一时间送达! 作者:穷码农 来源:https://zhuanlan.zhihu.com/p/10 ...

  8. LeetCode Construct the Rectangle

    原题链接在这里:https://leetcode.com/problems/construct-the-rectangle/ 题目: For a web developer, it is very i ...

  9. python刷leetcode_零基础python刷leetcode -- 3. Longest Substring Without Repeating Characters

    算法很重要,但是每天也需要学学python,于是就想用python刷leetcode 的算法题,和我一起开始零基础python刷leetcode之旅吧.如有不对的地方,希望指正,万分感谢~~ 题目 最 ...

最新文章

  1. 解决vue: 无法将“vue”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称...
  2. 在Ubuntu下成功搭建以太坊私有链挖矿并转账
  3. 一篇文章来告诉你可视化是多么重要
  4. Mybatis学习IDEA(1)-环境搭建以及入门案例
  5. 15大统计数据描绘网络安全行业市场蓝图
  6. idea导入servlet包还是显示红色_还是从servlet 开始吧,hello servlet
  7. 牛逼!9种不同的方法帮助你提高国内访问Github的速度到2MB/s以上...
  8. 扑克牌斗牛游戏的概率问题
  9. html,css使用表格制作课程表
  10. 【上电即上华为云】华为云smart智联PLC无线网关_plc_3121N-H(3121N-IED)
  11. Java项目:SSH学生请假管理系统
  12. QGis加载网络地图
  13. 百度快照被劫持百度网址安全中心提醒您:该页面可能已被非法篡改
  14. 数据中心云工作流任务的能效调度算法研究(1)
  15. js中some和every用法
  16. Xilinx_RAM_IP核的使用
  17. 利用熵权法确定指标权重
  18. 生物信息分析:从入门到精通结语
  19. Exce丨服务器2O19系统己满,《电子表格Exce20.docx
  20. 阿里云备案流程(全新备案)

热门文章

  1. led显示屏p10参数设置_LED显示屏全彩P10的详细参数及说明
  2. linux中默认安装php,Linux系统中Apache PHP MySQL的默认安装路径
  3. 什么叫组网_5G中,啥是独立组网(SA)与非独立组网(NSA)
  4. TCP/UDP 常用端口列表汇总
  5. 快速云原生化,从数据中心到云原生的迁移实践
  6. 快速部署 Spring PetClinic 到函数计算平台
  7. bp神经网络pid控制_文章推荐 | BP神经网络PID控制器在无人机编队飞行中的应用...
  8. c语言共有几种运算符_【填空题】C语言一共有 ()个关键字,()中控制语句,()种运算符...
  9. mybatis 自动生成integer_Intellij IDEA 中使用 MyBatis-generator 自动生成 MyBatis代码
  10. 发布json数据_数据库每周国际新闻 20201211