Find the Difference -- LeetCode
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的更多相关文章
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- [LeetCode] Minimum Time Difference 最短时间差
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...
- [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- LeetCode——Find the Difference
LeetCode--Find the Difference Question Given two strings s and t which consist of only lowercase let ...
- LeetCode Minimum Time Difference
原题链接在这里:https://leetcode.com/problems/minimum-time-difference/description/ 题目: Given a list of 24-ho ...
- [Leetcode Week10]Minimum Time Difference
Minimum Time Difference 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-time-difference/desc ...
- LeetCode Minimum Absolute Difference in BST
原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...
- LeetCode 1026. Maximum Difference Between Node and Ancestor
原题链接在这里:https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/ 题目: Given the ro ...
- 【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...
随机推荐
- ext radiogroup如何取值和设值
var radios = Ext.create('Ext.form.Panel', { title: 'RadioGroup Example', width: 300, height: 125, bo ...
- SpringMVC学习 -- ModelAndView , Model , ModelMap , Map 及 @SessionAttributes 的使用
输出模型数据: ModelAndView:处理方法返回值类型为 ModelAndView 时 , 其中包含视图和模型信息.方法体即可通过该对象添加模型数据 , 即 SpringMVC 会把 Model ...
- javascript实现瀑布流效果(固定宽度)
HTML代码: <div id="content"> <div class="box"> <div class="img ...
- 帮助小伙伴写的组装xml字符串类
import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.uti ...
- bzoj 1096 斜率优化DP
首先比较容易的看出来是DP,w[i]为前i个工厂的最小费用,那么w[i]=min(w[j-1]+cost(j,i))+c[i],但是这样是不work的,复杂度上明显过不去,这样我们考虑优化DP. 设A ...
- poj 2312 Battle City(优先队列+bfs)
题目链接:http://poj.org/problem?id=2312 题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟. ...
- [Leetcode Week11]Kth Largest Element in an Array
Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...
- pymysql 线程安全pymysqlpool
# -*-coding: utf-8-*- # Author : Christopher Lee # License: Apache License # File : test_example.py ...
- 【LA3461】Leonardo的笔记本
白书例题. #include<bits/stdc++.h> using namespace std; ],vis[],cnt[]; inline int read(){ ,x=;char ...
- 【反演复习计划】【COGS2432】爱蜜莉雅的施法
也是一个反演. 第一次手动推出一个简单的式子,激动.jpg 原题意思是求:$Ans=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\phi(gcd(i,j))$随 ...