How to get the number of characters that two strings have in common in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Sub Main(ByVal args As String())
        Dim str1 As String = "abcdefg"
        Dim str2 As String = "xayzgoe"
		
        Dim count As Integer = CommonCharactersCount(str1, str2)
        
		Console.WriteLine(count)
    End Sub

	Public Shared Function CommonCharactersCount(ByVal str1 As String, ByVal str2 As String) As Integer
        Dim set1 As HashSet(Of Char) = New HashSet(Of Char)(str1)
        Dim set2 As HashSet(Of Char) = New HashSet(Of Char)(str2)
		
        set1.IntersectWith(set2)
        
		Return set1.Count
    End Function
End Class



' run:
'
' 3
'

 



answered Mar 20 by avibootz
...