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.

本题最直观的思路为排序后对s和t逐位进行比较。故得以下代码:

 class Solution
{
public:
char findTheDifference(string s, string t)
{
sort(s.begin(), s.end());
sort(t.begin(), t.end());
for(int i = ; i < s.size(); i++)
{
if(s[i] == t[i])
{
continue;
}
else
{
return t[i];
}
}
return t[t.size() - ];
}
};

由于排序复杂度过高,于是查询别的解法发现,可以建立一个字母表,s出现一次字母表对应位置+1,t中出现一次对应位置-1。那么只在t中出现的字母的位置为-1。代码如下:

 public char findTheDifference(String s, String t) {
if(s == null || s.length() == )
return t.charAt();
int[] letters = new int[];
for(int i = ; i < s.length(); i++){
int sPosition = s.charAt(i) - 'a';
int tPosition = t.charAt(i) - 'a';
letters[sPosition]++;
letters[tPosition]--;
}
int tPosition = t.charAt(t.length()-) - 'a';
letters[tPosition]--;
char res = 'a';
for(int i = ; i < letters.length; i++){
if(letters[i] == -){
res+= i;
break;
}
}
return res;
}

LeetCode 389. Find the Difference的更多相关文章

  1. LeetCode 389. Find the Difference (找到不同)

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

  2. 9. leetcode 389. Find the Difference

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

  3. LeetCode 389 Find the Difference 解题报告

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

  4. LeetCode - 389. Find the Difference - 三种不同解法 - ( C++ ) - 解题报告

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

  5. LeetCode: 389 Find the Difference(easy)

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

  6. LeetCode之389. Find the Difference

    -------------------------------------------------- 先计算每个字母的出现次数然后减去,最后剩下的那一个就是后来添加的了. AC代码: public c ...

  7. 【LeetCode】389. Find the Difference 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:异或 方法三:排序 日 ...

  8. 【LeetCode】389 Find the Difference(java)

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

  9. [LeetCode&Python] Problem 389. Find the Difference

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

随机推荐

  1. 【转载】--仅用 []()+! 就足以实现几乎任意Javascript代码

    最近在做CTF的题目,遇见了一个都是[]()+!这样的文件,于是百度了一下,发现这个博客对这个有解释. G Reader里Dexter同学的分享,来自sla.ckers.org的又一神作 点我测试 G ...

  2. Timequest收集命令

    表 1. 收集命令 命令 说明 all_clocks 返回设计中所有时钟的收集. all_inputs 返回设计中输入端口的收集. all_outputs 返回设计中所有寄存器的收集. get_cel ...

  3. spring boot开发入门

    最近学习了spring boot,刚刚入门,整理下

  4. Javascript设计模式学习一

    学习Javascript设计模式之前,需要先了解一些相关知识,面向对象的基础知识.this等重要概念,以及掌握一些函数式编程的技巧. Js多态 多态的思想:实际上是把“做什么”和“谁去做”分离开来.例 ...

  5. bind绑定参数

    const curry = (fn) => (...args)=>fn.bind(null,...args); const split = curry((splitOn, str) =&g ...

  6. Python学习笔记—Python基础1 介绍、发展史、安装、基本语法

    第一周学习笔记: 一.Python介绍      1.Python的创始人为吉多·范罗苏姆.1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言 ...

  7. java开发人员,最应该学习和熟练使用的工具类。google guava.(谷歌 瓜娃)

    学习参考文章: http://blog.csdn.net/wisgood/article/details/13297535 http://ifeve.com/google-guava/ http:// ...

  8. Integer与int的区别(包装类和基本数据类型的区别)

    1. 默认值 int默认值为0,Integer的默认值为null.推论:Integer既可以表示null又可以表示0 2. 包装类中提供了该类型相关的很多算法操作方法 如把十进制装换为2进制(toBi ...

  9. BZOJ 3771: Triple

    Description 问所有三/二/一元组可能形成的组合. Sol FFT. 利用生成函数直接FFT一下,然后就是计算,计算的时候简单的容斥一下. 任意三个-3*两个相同的+2*全部相同的+任意两个 ...

  10. linux 软连接和硬链接

    硬链接 ln sourceFile targetFile 硬链接只能给文件创建,不能为目录建立硬链接,硬链接不能跨分区创建,  硬链接会增加inode连接数, 硬链接的文件删除不影响其他文件 课外: ...