389. Find the Difference

Input: s = "abcd", t = "abcde"
Output: "e"
Explanation: 'e' is the letter that was added.
Input: s = "", t = "y"
Output: "y"
  • 0 <= s.length <= 1000
  • t.length == s.length + 1
  • s and t consist of lowercase English letters.
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
dict_=dict()
for i in s:
if i in dict_:
dict_[i]+=1
else:
dict_[i]=1

for i in t:
if(i not in dict_ or dict_[i]==0):
return i
else:
dict_[i]-=1
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
s=sorted(s)
t=sorted(t)
for i in range(len(s)):
if s[i]!=t[i]:
return t[i]
return t[-1]
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
tot=0
for i in range(len(t)):
tot+=ord(t[i])
if(i<len(s)):
tot-=ord(s[i])
return(chr(tot))
#time =O(len(t))
#space = O(1)
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
xor_=0
for i in range(len(s)):
xor_^=ord(s[i])^ord(t[i])
return(chr(xor_^ord(t[-1])))

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.