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.
class Solution {
public char findTheDifference(String s, String t) {
//in the middle(start) or in the end
//get small length
int sn = s.length();
int tn = t.length();
if(sn > tn)
return helper(s,t);
else return helper(t,s);
}
char helper(String l, String s){ // larger and smaller
int[] a = new int[26];
int[] b = new int[26];
for(int i = 0; i<l.length(); i++){
a[l.charAt(i) - 'a'] ++;
}
for(int i = 0; i<s.length(); i++){
b[s.charAt(i) - 'a'] ++;
}
for(int i = 0; i<26; i++){
if(a[i] != b[i]) return (char)(i+'a');
}
return 'a';
}
}

*389. Find the Difference (string + map(26)) read problems carefully的更多相关文章

  1. expected number,sequence,or string.map evaluated instead of freemarker.template.smplehash

    expected number,sequence,or string.map evaluated instead of freemarker.template.smplehash 使用freemark ...

  2. SONObjetc和String Map Bean互转,JSONArray和String List互转

    import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...

  3. 总结的一些json格式和对象/String/Map/List等的互转工具类

    总结的一些json格式和对象/String/Map/List等的互转工具类,有需要的可以看看,需要引入jackson-core-asl-1.7.1.jar.jackson-jaxrs-1.7.1.ja ...

  4. Map<String, String> map按key值排序

    private static String buildMd5Params(Map<String, String> map) { StringBuilder result = new Str ...

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

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

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

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

  7. LeetCode之389. Find the Difference

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

  8. Total Difference String

    Total Difference Strings 给一个string列表,判断有多少个不同的string,返回个数相同的定义:字符串长度相等并从左到右,或从右往左是同样的字符 abc 和 cba 为视 ...

  9. hdu-1251(string+map)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 思路:重点是用gets输入,而且用a[20],不能直接输入string类型的. #include ...

随机推荐

  1. UVALive 3645 时序模型

    按航班拆点 注意返边的条件 #include<bits/stdc++.h> using namespace std; const int maxn = 1e6+11; const int ...

  2. c#生产/消费RabbitMQ

    public sealed class JsonSerializer { public static byte[] Serialize(object message) { return Encodin ...

  3. gulp不压缩打包layui

    从网上下载的layui都是压缩包,如何打包在一个文件且不压缩呢?如下方法: 1.https://gitee.com/sentsin/layui下载源码(本文的为2.4.5版本) 2.安装nodejs( ...

  4. element-ui表格合并span-method

    先看一下合并后的样式,表格第二行,二三四列合并 官网给我们提供了span-method的方法可以进行表格合并,有4个参数返回:row,column,rowIndex,columnIndex;row和c ...

  5. my.宠物升级79级

    1.蚌仙子 74级半不到的时候,吃  月华露(500000经验的那种)  吃3个 正好 79级半 我记得 之前 是升满75级 再吃月华露 到79级的时候 经验溢出了.不突破还好,突破的话 宠物就升级了 ...

  6. Spring---数据缓存(未完待续)

    1.为什么需要数据缓存? 程序的瓶颈大都在数据库,而内存的速度是远远大于硬盘的,当我们需要重复读取相同数据时,一次又一次的请求数据库或者远程服务,导致大量的时间浪费在数据库或者 远程服务上,导致程序性 ...

  7. hadoop和spark比较

    http://blog.51cto.com/13943588/2165946 3.hadoop和spark的都是并行计算,那么他们有什么相同和区别?  两者都是用mr模型来进行并行计算,hadoop的 ...

  8. jeecg权限设置案例

    JEECG简单实例讲解权限控制 一.业务背景 某公司要实现一个日志系统,用来了解员工的工作量饱和情况. 二.需求 1.角色分为:员工.经理两种. 2.员工每天在日志系统中填报工作总结,然后经理进行点评 ...

  9. python单元测试框架-unittest(一)

    简介 unittest单元测试框架不仅可以适用于单元测试,还可以使用WEB自动化测试用例的开发与执行,该测试框架可组织执行测试用例,并且提供了丰富的断言方法,判断测试用例是否通过,最终生成测试结果. ...

  10. [转]jQuery TextBox Water Mark with asp.net

    本文转自:http://naspinski.net/post/jQuery-TextBox-Water-Mark-with-aspnet.aspx I stole majority of this c ...