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;
}
};

LeetCode——Find the Difference的更多相关文章

  1. [LeetCode] Minimum Time Difference 最短时间差

    Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...

  2. [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  3. LeetCode Minimum Time Difference

    原题链接在这里:https://leetcode.com/problems/minimum-time-difference/description/ 题目: Given a list of 24-ho ...

  4. LeetCode Minimum Absolute Difference in BST

    原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...

  5. LeetCode 1026. Maximum Difference Between Node and Ancestor

    原题链接在这里:https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/ 题目: Given the ro ...

  6. [LeetCode] Find the Difference 寻找不同

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  7. [LeetCode] Find the Difference

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  8. LeetCode:Find the Difference_389

    LeetCode:Find the Difference [问题再现] Given two strings s and t which consist of only lowercase letter ...

  9. LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

随机推荐

  1. Http服务器实现文件上传与下载(二)

    一.引言 欢迎大家接着看我的博客,如何大家有什么想法的话回复我哦,闲话不多聊了,接着上一讲的内容来说吧,在上一节中已经讲到了请求头字符串的解析,并且在解析中我我们已经获取了url.就是上节中提到的/d ...

  2. Java RSA加密以及验签

    签名加密以及验签工具类: 一般秘钥分为3个key 1.自己生成的私钥, 2.通过私钥生成的公钥1 3.通过提交公钥1给某宝,获取的公钥2. RSA公钥加密算法简介 非对称加密算法.只有短的RSA钥匙才 ...

  3. 诡异的js

    [] + {}; 隐式转换后,是0 那 {} + []呢? var a = 42,b; b = ( a++, a);

  4. JavaScript 学习(2)表单元素

    ##JavaScript 学习-2 1. 表单和表单元素 1.1 form对象 form对象的引用:document.forms[0]或者引用name属性,如:document.forms[" ...

  5. 定时备份DB和WEB文件

    sql.sh #!/bin/bash ##备份数据库: 每4小时 date2=`date "+%Y-%m-%d---%H.%M.%S"` /alidata/server/mysql ...

  6. jenkins, docker-composer

      http://ju.outofmemory.cn/entry/240867

  7. 模块 - random/string/os/sys/shutil/zipfile/tarfile

    random 模块 方法: >>> random.randint(1,3) #会包含 1 2 3 3 >>> random.randrange(1,3) #会包含 ...

  8. java基础09 数组的使用

    /** * 求数组中的最大值 */ @Test public void test14(){ //定义一个数组 参赛的选手 int [] nums={50,20,30,80,100,90}; //定义一 ...

  9. Django的模型层(2)- 多表操作(上)

    一.创建模型 例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对一(on ...

  10. 初识Locust---认识

    性 能测试工具: 基于Python的性能测试工具-locust 现在性能测试方面有很多测试工具,比如我们熟悉的loadrunner.jmeter.ab等,用过的也就是这几种,如果是学过这些工具的可能对 ...