[C#][LeetCode][Easy] 389. Find the Difference

心得:

題目要求找出字串變數s與t差異的字母,變數t一定大於變數s且都為小寫,跑兩個迴圈就解決了。

問題:

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.
答案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

public class Solution {
public char FindTheDifference(string s, string t) {
for (int i = 0; i < s.Length; i++)
{
for (int j = 0; j < t.Length; j++)
{
if (s[i] == t[j])
{
t = t.Remove(j, 1);
break;
}
}
}

return t.Length == 1 ? t[0] : new char();
}
}