Isomorphic Strings - Leetcode 205 - Python

preview_player
Показать описание


0:00 - Read the problem
0:45 - Drawing Explanation
6:32 - Coding Explanation

leetcode 205

#coding #interview #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

such an easy solution, i was reading this problem and just had no idea what isomorhpic was or how to even remotely solve it. thanks bro

raghavdewangan
Автор

Your videos are seriously top notch, there are so many other tutorials out there that essentially read the code line by line. But you provide an intuitive approach to how to solve the problem, in a way that similar approach can also be applied to similar problems.

I'm going to keep watching most likely all of your videos and most likely subscribe on your website as well just to be able to go through the material. The quality really speaks for itself

Thank you

gerryramosftw
Автор

Great Work! I love your videos. I often watch them to practice my English Speaking by repeating your explanations. It really helps. For this question, you can use ONE DICTIONARY and check duplicates in its values.

class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
match = dict()
for index in range(len(s)):
if s[index] not in match:
match[s[index]] = t[index] # new map
else:
if t[index] != match[s[index]]: # check existing map
return False

values = match.values()
return len(set(values)) == len(values) # check if any duplicates

hudsonriverrunner
Автор

In my opinion, your explanation is brilliant, Thank you a lot!

ОленаЄфименко-бю
Автор

I am addicted to your channel. great work

YasirYSR
Автор

Om pretty sure you can use one map and store em in key val pair and check if the current char in on of string != to the prev value in the other str

bg-sjtx
Автор

Thanks for your intuition, which inspired me to come out with an approach to this problem. I tried Try and Except and it works too.

LunaFlahy
Автор

Amazing question..this is why Facebook is my favorite tool to generate isomorphic strings.

surajnair
Автор

You need only one dict for this task.
just check, that account of unique characters is the smame

class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
d = {}
for i, n in enumerate(s):
if n not in d: d[n]=t[i]
else:
if d[n] != t[i]:
return False
return len(d.keys()) == len(set(d.values()))

yustas
Автор

i couldn't understand the question from leetcode so i had to come here.
Thank you for making me understand

tawkeer
Автор

you can do it without reverse map. if you do check both strings: isIsomorphic(s, t) && isIsomorphic(t, s) - makes impl simpler!

dwarfling
Автор

Using this code it seems to return true for "babc"/"baba". If I'm reading this right, it doesn't seem to do a check on the last character.

CloudPrince
Автор

here is my solution
def isomerphic(a, b):
map1=[]
map2=[]
for idx in a:
map1.append(a.index(idx))
for idx in b:
map2.append(b.index(idx))
print(map1)
print(map2)
if map1==map2:
return True
return False
a=("bar")
b=("foo")
print(isomerphic(a, b))

tanmaydash
Автор

in 3:29 you could just use this example :
s =
"badc"
t =
"baba"

PrateekDas-ynce
Автор

Suggestion: Reconstruct Itinerary (Graph)

jared
Автор

Great Explanation. I'm wonder if there is any other Optimal solution.

bandhammanikanta
Автор

Thank you. I finally understand what is isomorphic haha. pretty good job man. thank you so much.

AndresMForero
Автор

Not Working Your code ....giving error in 3rd example of paper to title
what to do ?

kushagrabhadauria
Автор

if i'm not mistaken, is time complexity O(n) and space complexity O(n) as well?

LamNguyen-nmid
Автор

is it O(1) space as worst case the dictionary will always be of size 128 (ascii)?

pacomarmolejo