389. Find the Difference
- Total Accepted: 22851
- Total Submissions: 46129
- Difficulty: Easy
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.
给你两个字符串s和t,t比s多了一个字符,问这个字符是什么。
可以用数组做Hash,也可以用HaspMap,但是java的Map数组我调了好久都不对,还是C++好用感觉= =+,最后还是用数组模拟的
public class Solution { public char findTheDifference(String s, String t) { int a[] = new int [26]; int len_s = s.length(); int len_t = t.length(); for(int i=0;i<len_s;i++){ a[s.charAt(i)-'a']++; } for(int i=0;i<len_t;i++){ a[t.charAt(i)-'a']--; if(a[t.charAt(i)-'a'] == -1){ return t.charAt(i); } } return ' '; } }