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';
}
}

最新文章

  1. dos 操作显示 &gt; nul 2&gt;nul
  2. 【原】常见CSS3属性对ios&amp;android&amp;winphone的支持
  3. 导出Excel之Epplus使用教程1(基本介绍)
  4. php文件遍历
  5. Android 闹钟设置
  6. Java调用摄像头截图
  7. pgsql获取表相关数据
  8. IOS隐藏navigationItem左右按钮的方法
  9. 《JAVASCRIPT高级程序设计》第五章(1)
  10. CSS--使用方式
  11. sso系统使用
  12. 【转廖大神】package.json 包安装
  13. [mysql,2018-02-28] bat安装、启动mysql,并创建数据库、表
  14. 【POI每日题解 #7】TES-Intelligence Test
  15. 使用 IntelliTrace 调试应用程序
  16. [UE4]添加蒙太奇动画
  17. DB2 SQL1477N问题
  18. sql server中的怎么把数值型转换为字符串
  19. U盘装系统:安装GHOST Win7系统教程
  20. linux-常用命令备注

热门文章

  1. linux 数据库管理
  2. UntraEdit 语法高亮 (MSSQL)
  3. ckeditor(在线文本编辑器)使用教程
  4. python 自动安装工具 setuptools(easy_install) 的使用
  5. 深入学习webpack(四)
  6. Jersey实现文件上传下载
  7. REST API 自动化测试 利器Rest Assured(API接口自动化测试框架体系)
  8. JDK一键安装,配置环境
  9. 一、简单gridview列表展示
  10. HttpClient4.3.3 禁止自动重定向