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. django 母版与继承

    在子页面的页面最上方使用下面的语法来继承母版. {% extends 'layouts.html' %} 块: 通过在母版中使用{% block xxx %}来定义块.在子页面中通过定义母版中的blo ...

  2. python 多线程概念

    ######多线程##### 什么是线程: 线程是操作系统能够进行运算调度的最小单位(程序执行流的最小单元).它被包含在进程之中,是进程中的实际运作单位. 一个进程中可以并发多个线程,每条线程并行执行 ...

  3. ubuntu php 连接sql server

    1.下载最新的freetds ,访问 http://www.freetds.org/, 或者在 ubuntu上用 wget ftp://ftp.freetds.org/pub/freetds/stab ...

  4. js网页瀑布流布局

    瀑布流布局思路: 1.css样式,图片的父级div样式设置为定位或者浮动 2.找出图片父级元素(box)和最外元素(main):获取box的宽度和main的宽,然后计算main容器一行能容纳多少个bo ...

  5. python学习2(转载)

    一.流程控制之while循环 语法:while 条件: 循环体else: else语句(当条件不成立的时候执行这里 和break没关系) 判断条件是否成立. 如果成立执行循环体.然后再次判断条件,.. ...

  6. 转 Django中的Form

    https://www.cnblogs.com/chenchao1990/p/5284237.html Form 一.使用Form Django中的Form使用时一般有两种功能: 1.生成html标签 ...

  7. 笔记本Win8 换Win7 设置 BIOS

    去年买了台笔记本,笔记本自带win8系统,想安装Win7折腾了好久都没有安装成功 后来在BIOS中找到了一个uefi/legacy boot项,将原来的uefi only 修改为legacy only ...

  8. android Activity启动过程(四)startActivityUncheckedLocked

    final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord, IVoiceInteract ...

  9. [转]jQuery实现图片轮播效果,jQuery实现焦点新闻

    本文转自:http://blog.csdn.net/tsyj810883979/article/details/8986157 效果图: 实现代码: <!DOCTYPE html> < ...

  10. linux命令strings

    linux命令strings,其man信息如下:strings(1)                                        GNU Development Tools      ...