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.

思路:用map记录所有char出现的次数。复杂度O(N)。

 class Solution {
public:
char findTheDifference(string s, string t) {
unordered_map<char, int> dict;
for (int i = ; i < s.size(); i++)
if (!dict.count(s[i])) dict.insert(make_pair(s[i], ));
else dict[s[i]]++;
for (int i = ; i < t.size(); i++)
if (!dict.count(t[i]) || !dict[t[i]]) return t[i];
else dict[t[i]]--;
return '\0';
}
};

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

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

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

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

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

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

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

  4. LeetCode——Find the Difference

    LeetCode--Find the Difference Question Given two strings s and t which consist of only lowercase let ...

  5. LeetCode Minimum Time Difference

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

  6. [Leetcode Week10]Minimum Time Difference

    Minimum Time Difference 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-time-difference/desc ...

  7. LeetCode Minimum Absolute Difference in BST

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

  8. LeetCode 1026. Maximum Difference Between Node and Ancestor

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

  9. 【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...

随机推荐

  1. rest与restful

      知乎上面摘抄的,感觉不错,分享下:  https://www.zhihu.com/question/28557115 1. REST描述的是在网络中client和server的一种交互形式:RES ...

  2. idea中mybatis-plugin破解

    Mybatis Plugin 一.Mybatis Plugin插件是什么 提供Mapper接口与配置文件中对应SQL的导航 编辑XML文件时自动补全 根据Mapper接口, 使用快捷键生成xml文件及 ...

  3. ActiveMQ(4) ActiveMQ JDBC 持久化 Mysql 数据库

    ActiveMQ 消息持久化机制: ActiveMQ 消息的持久化机制有 JDBC.AMQ.KahaDB 和 LevelDB,其中本示例版本(5.15.2)默认机制为 KahaDB.无论哪种持久化机制 ...

  4. supervisor提高nodejs调试效率

    1.NodeJS环境安装 2.安装supervisor npm install  supervisor -g (表示安装到全局路径下) 开发nodejs程序,调试的时候,无论你修改了代码的哪一部分,都 ...

  5. Hadoop之计数器与自定义计数器及Combiner的使用

    1,计数器: 显示的计数器中分为四个组,分别为:File Output Format Counters.FileSystemCounters.File Input Format Counters和Ma ...

  6. 【洛谷 P1653】 猴子 (并查集)

    题目链接 没删除调试输出,原地炸裂,\(80\)->\(0\).如果你要问剩下的\(20\)呢?答:数组开小了. 这题正向删边判连通性是很不好做的,因为我们并不会并查集的逆操作.于是可以考虑把断 ...

  7. 河南省第十届省赛 Plumbing the depth of lake (模拟)

    title: Plumbing the depth of lake 河南省第十届省赛 题目描述: There is a mysterious lake in the north of Tibet. A ...

  8. 密码框JPasswordField 的使用

    JPasswordField的主要方法为setEchoChar(char c),其中的字符C为回显字符. package first; import javax.swing.*; import jav ...

  9. (12)Linux shell之read 用法

    Linux shell之read 用法 #!/bin/bash#read 用来读取屏幕输入或是读取文件内容.read -p  "please input you  name: " ...

  10. 【洛谷P3709】大爷的字符串题

    看这题网上居然还没人写blog,怕是都去看洛谷自带的了-- 你才是字符串!你全家都是字符串!这题跟字符串没多大关系,只是出题人lxl想要吐槽某中学而已--... 其实这题说白了就是问区间里出现最多的数 ...