[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.

答案:

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();
    }
}


這裡的資訊對您有用嗎?歡迎斗內給我