Isomorphic Strings
Python
Medium
3 views
Problem Description
Input has two strings s and t are provided (same length). Two strings are isomorphic if each character in s can be mapped to exactly one character in t and vice-versa. Output YES or NO.
Input Format
Two lines: s then t.
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if len(lines)<2: sys.exit(0)
s=lines[0].strip()
t=lines[1].strip()
if len(s)!=len(t):
sys.stdout.write('NO')
else:
st={}
ts={}
ok=True
for a,b in zip(s,t):
if a in st and st[a]!=b:
ok=False
break
if b in ts and ts[b]!=a:
ok=False
break
st[a]=b
ts[b]=a
sys.stdout.write('YES' if ok else 'NO')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!