DEV Community

Shailesh Kumar
Shailesh Kumar

Posted on

Find the Difference Between Two Strings | LeetCode Day 24

Problem

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.

Solution -

from collections import Counter
class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        c1 = Counter(s)
        c2 = Counter(t)
        for k2, v2 in c2.items():
            v1 = c1.get(k2, 0)
            if v1 != v2:
                return k2

Top comments (0)