LeetCode 389. Find the Difference

Post by ailswan Dec. 13, 2023

389. Find the Difference

Problem Statement

link: LeetCode.cn LeetCode

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.

Example:

Input: s = "abcd", t = "abcde" Output: "e"

Input: s = "", t = "y" Output: "y"

Solution Approach

Algorithm

Implement

    class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
      sl = sorted(s) + [" "]
      tl = sorted(t)
      for (c1, c2) in zip(sl, tl):
        if c1 != c2:
          return c2