Leetcode練習(Python):第389題:找不同:給定兩個字符串 s 和 t,它們只包含小寫字母。 字符串 t 由字符串 s 隨機重排,然后在隨機位置添加一個字母。 請找出在 t 中被添加的字母。


題目:

找不同:給定兩個字符串 s 和 t,它們只包含小寫字母。  字符串 t 由字符串 s 隨機重排,然后在隨機位置添加一個字母。  請找出在 t 中被添加的字母。

示例:

輸入:
s = "abcd"
t = "abcde"

輸出:
e

解釋:
'e' 是那個被添加的字母。

思路:

思路較簡單。

程序:

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        if not s and t:
            return t 
        auxiliary1 = []
        auxiliary2 = []
        result = ''
        for index1 in range(len(s)):
            auxiliary1.append(s[index1])
        for index2 in range(len(t)):
            auxiliary2.append(t[index2])
        auxiliary1 = sorted(auxiliary1)
        auxiliary2 = sorted(auxiliary2)
        auxiliary1.append('')
        for index3 in range(len(auxiliary1)):
            if auxiliary1[index3] != auxiliary2[index3]:
                result += auxiliary2[index3]
                break
        return result

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM