LeetCode 389. Find the Difference
考点 | 难度 |
---|---|
Hash Table | Easy |
题目
You are given two strings s
and t
.
String t
is generated by random shuffling string s
and then add one more letter at a random position.
Return the letter that was added to t
.
思路
不需要存储s
和t
里面的字母。
答案
public char findTheDifference(String s, String t) {
int total = 0;
for(int n:s.toCharArray())
total+=n;
for(int n:t.toCharArray())
total-=n;
return (char)Math.abs(total);
}