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. Go语言关键字之1--range

    https://blog.csdn.net/iamlihongwei/article/details/78842857 https://studygolang.com/articles/1952 ht ...

  2. TreeMap读源码总结

    红黑树: 定义 A red–black tree is a kind of self-balancing binary search tree in computer science. Each no ...

  3. tomcat启动非常慢

    解决: 有两种解决办法: 1)在Tomcat环境中解决 可以通过配置JRE使用非阻塞的Entropy Source. 在catalina.sh中加入这么一行: JAVA_OPTS="-Dja ...

  4. ctrip-apollo

    云端多网卡问题: 参考:https://blog.csdn.net/buyaore_wo/article/details/79847404

  5. Selenium打开IE报错“Protected Mode settings...”解决方法

    最近在使用Selenium打开IE浏览器碰到以下报错:

  6. maya2014安装失败如何卸载重装

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  7. MySQL · 引擎特性 · InnoDB index lock前世今生

    http://mysql.taobao.org/monthly/2015/07/05/ MySQL · 引擎特性 · InnoDB index lock前世今生 前言 InnoDB并发过程中使用两类锁 ...

  8. [转]IE和FireFox中JS兼容之event .

    转载于:http://blog.csdn.net/jiachunfeng/article/details/6448186 http://justcoding.iteye.com/blog/587876 ...

  9. [转]将input file的选择的文件清空

    本文转自:http://hi.baidu.com/xiongshihu/item/125c79b47632e794194697f5 上传文件时,选择了文件后想清空文件路径的两种办法: JS代码 < ...

  10. 021-动态生成验证码jsp代码模板

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...