*389. Find the Difference (string + map(26)) read problems carefully
2024-09-03 23:37:51
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';
}
}
最新文章
- dos 操作显示 >; nul 2>;nul
- 【原】常见CSS3属性对ios&;android&;winphone的支持
- 导出Excel之Epplus使用教程1(基本介绍)
- php文件遍历
- Android 闹钟设置
- Java调用摄像头截图
- pgsql获取表相关数据
- IOS隐藏navigationItem左右按钮的方法
- 《JAVASCRIPT高级程序设计》第五章(1)
- CSS--使用方式
- sso系统使用
- 【转廖大神】package.json 包安装
- [mysql,2018-02-28] bat安装、启动mysql,并创建数据库、表
- 【POI每日题解 #7】TES-Intelligence Test
- 使用 IntelliTrace 调试应用程序
- [UE4]添加蒙太奇动画
- DB2 SQL1477N问题
- sql server中的怎么把数值型转换为字符串
- U盘装系统:安装GHOST Win7系统教程
- linux-常用命令备注