问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4062 访问。

给定两个字符串 s 和 t,它们只包含小写字母。

字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

请找出在 t 中被添加的字母。

输入:
s = "abcd"
t = "abcde"

输出:
e

解释:'e' 是那个被添加的字母。


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.

Input:
s = "abcd"
t = "abcde"

Output:
e

Explanation:'e' is the letter that was added.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4062 访问。

public class Program {public static void Main(string[] args) {var s = "ecb";var t = "beca";var res = FindTheDifference(s, t);Console.WriteLine(res);s = "loveleetcode";t = "loveleetxcode";res = FindTheDifference2(s, t);Console.WriteLine(res);Console.ReadKey();}private static char FindTheDifference(string s, string t) {var cs = s.ToArray();Array.Sort(cs);var ct = t.ToArray();Array.Sort(ct);var i = 0;for(; i < cs.Length; i++) {if(cs[i] != ct[i]) return ct[i];}return ct[i];}private static char FindTheDifference2(string s, string t) {var dic = new Dictionary<char, int>();foreach(var c in s) {if(dic.ContainsKey(c)) {dic[c]++;} else {dic[c] = 1;}}foreach(var c in t) {if(dic.ContainsKey(c)) {dic[c]--;if(dic[c] < 0) return c;} else {return c;}}return ' ';}}

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4062 访问。

a
x

分析:

FindTheDifference 的时间复杂度基于排序所使用的排序算法,FindTheDifference2 的时间复杂度为: 

C#LeetCode刷题之#389-找不同(Find the Difference)相关推荐

  1. C#LeetCode刷题之#744-寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4001 访问. 给定一个只包含小写字母的有序数组letters 和 ...

  2. C#LeetCode刷题之#724-寻找数组的中心索引( Find Pivot Index)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3742 访问. 给定一个整数类型的数组 nums,请编写一个能够返 ...

  3. C#LeetCode刷题-位运算

    位运算篇 # 题名 刷题 通过率 难度 78 子集 67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 1 ...

  4. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

  5. ​LeetCode刷题实战391:完美矩形

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  6. C#LeetCode刷题-二分查找​​​​​​​

    二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

  7. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  8. LeetCode刷题笔记汇总

    LeetCode刷题笔记汇总 第一次刷LeetCode写的一些笔记. 1.两数之和 3.无重复字符的最长子串 15.三数之和 18.四数之和 19.删除链表的倒数第 N 个结点 20.有效的括号 21 ...

  9. LeetCode 刷题之路(python版)

    摘自:https://blog.csdn.net/qq_32384313/article/details/90745354 LeetCode 刷题之路(python版) 小坏wz 2019-06-02 ...

最新文章

  1. 亚洲与非洲:中国支付巨头的海外进击
  2. Google Android开发精华教程
  3. 五天带你学完《计算机网络》·第五天·网络层(下)
  4. unescape解密 php,php 版 模仿 js ,unescape函数解码,escape函数编码的方法
  5. 额尔古纳的俄罗斯女孩
  6. 共享文件夹不能访问的问题解决
  7. js 如何去除字符两端的引号
  8. 【Hihocoder - offer编程练习赛93 套题题解】交错01串(贪心,暴力)方格矩阵高度(模拟)数对(STLmultiset)修整土地(网络流)
  9. Struts2.3,s:iterator,c:forEach遍历map中的list集合
  10. 恒丰银行年报:以区块链等线上“大脑”再造业务流程
  11. (转)李开复哥伦比亚大学演讲:如何才能不错过人工智能时代
  12. 数据包络分析DEA有哪些指标?
  13. laydate设置起始时间,laydate设置开始时间和结束时间
  14. 关于godot第三方功能增强版
  15. 安装VMware的VM Tools
  16. java改变数据库配置文件信息_JAVA应用修改数据库链接信息一般在哪个配置文件中?...
  17. C语言实现TCP网络通信
  18. Kvaser Memorator Professional五通道CAN/CANFD总线分析记录仪
  19. Azure: Azure AD(For Development)的使用
  20. 单片机工程师笔试题目归纳汇总

热门文章

  1. C/C++——++i、i+=1、i++之间的区别(附在学习boost时遇到的例子)
  2. 案例 月工作列表 c# 1614099857
  3. fastdfs-配合nginx-麻烦的
  4. django-session了解
  5. linux指令:echo, head, tail
  6. go标准库的学习-encoding/json
  7. 释放技术红利 阿里云存储服务降价15%
  8. Linux开发商计划停止开发32位版本
  9. 2017年预测:突破性创意工作站、物联网
  10. 《基于模型的软件开发》——1.2 结构化开发