' Two strings are isomorphic if characters in one can be replaced to get the other,
' while preserving the order.
Imports System
Imports System.Collections.Generic
Module Program
Function AreIsomorphic(s As String, t As String) As Boolean
If s.Length <> t.Length Then Return False
Dim mapST As New Dictionary(Of Char, Char)()
Dim mapTS As New Dictionary(Of Char, Char)()
For i As Integer = 0 To s.Length - 1
Dim c1 As Char = s(i)
Dim c2 As Char = t(i)
' Check mapping from s → t
If mapST.ContainsKey(c1) Then
If mapST(c1) <> c2 Then Return False
Else
mapST(c1) = c2
End If
' Check mapping from t → s
If mapTS.ContainsKey(c2) Then
If mapTS(c2) <> c1 Then Return False
Else
mapTS(c2) = c1
End If
Next
Return True
End Function
Sub Main()
Console.WriteLine(AreIsomorphic("egg", "add"))
Console.WriteLine(AreIsomorphic("foo", "bar"))
Console.WriteLine(AreIsomorphic("paper", "title"))
End Sub
End Module
' run:
'
' True
' False
' True
'