描述
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.
解决
自己想的思路比较麻烦,用了hashmap,一点点的查找,看了别人的思路,比较简单一点,下面是别人的简单思路:
class Solution {
public char findTheDifference(String s, String t) {
int difference = (int)t.charAt(s.length());
for(int i = 0; i< s.length(); i++){
difference -= (int)s.charAt(i);
difference += (int)t.charAt(i);
}
return (char)difference;
}
}