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. PS常用快捷键大全

    察看图像类别 说明:: --- Shift键 : --- 空格键         *--- 在Imageready中不适用 § --- 只在Imageready中可用 动作 结果 双击工具箱::或Ct ...

  2. A. Cinema Line

    A. Cinema Line time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  3. 2048小游戏(Java)(swing实现)(二)

    这里是上一次的成果,只能用鼠标点,没法用键盘 最近扩充了一下知识面,实现了用键盘操控2048小游戏 但是还是不支持同时使用键盘和鼠标同时操作 import javax.swing.*; //impor ...

  4. thinkPHP5.0获取器

    获取器的作用是在获取数据的字段值后自动进行处理,例如,我们需要对状态值进行转换,可以使用: class Cate extends Model { public function getTypeAttr ...

  5. 生成jvm快照文件

    原文:https://blog.csdn.net/jijianshuai/article/details/79128033  -Xmx20m -Xms5m -XX:HeapDumpOnOutofMem ...

  6. Django开发常见问题

    1.Django设置中文,和时区.静态文件指向 #========================================================== # 设置时区 注意注释上面的:L ...

  7. spring mvc源码分析

    1.传统xml配置方式 web.xml里面配置:org.springframework.web.servlet.DispatcherServlet,处理项目的spring配置文件:classpath* ...

  8. free -m命令输出详解

    free -m输出有3行: Mem:表示物理内存 -/+ buffers/cached:表示物理内存缓存 Swap:表示硬盘交换分区 其中Mem中的total.used.free.shared.buf ...

  9. CAD安装失败怎样卸载CAD 2010?错误提示某些产品无法安装

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

  10. fireFox在中国的https网站的时候,老会出 ssl_error_unsupported_version 这个错误。

    fireFox在中国的https网站的时候,老会出 ssl_error_unsupported_version  这个错误. 出现在 这个的解决办法就是 在地址栏里输入 about:config 查找 ...