How to check if two strings are isomorphic in VB.NET

2 Answers

0 votes
' 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
' 

 



answered Dec 19, 2025 by avibootz
edited Dec 19, 2025 by avibootz
0 votes
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(255) As Integer
		Dim mapTS(255) As Integer

		For i As Integer = 0 To s.Length - 1
			Dim c1 As Integer = Convert.ToInt32(s(i))
			Dim c2 As Integer = Convert.ToInt32(t(i))

			If mapST(c1) <> mapTS(c2) Then Return False

			mapST(c1) = i + 1
			mapTS(c2) = i + 1
		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
' 

 



answered Dec 19, 2025 by avibootz
...