Imports System
Imports System.Collections.Generic
Public Class MostCommonPair_VB_NET
Public Shared Sub Main(ByVal args As String())
Dim s As String = "xzvxdeshaalzxzmdenlopxzxzxzaaqdewrzaaaapeerxzxz"
Console.WriteLine("The most common pair: " & FindMostCommonPair(s))
End Sub
Public Shared Function FindMostCommonPair(ByVal s As String) As String
Dim pairCount As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)()
For i As Integer = 0 To s.Length - 1 - 1
Dim pair As String = s.Substring(i, 2)
If pairCount.ContainsKey(pair) Then
pairCount(pair) += 1
Else
pairCount(pair) = 1
End If
Next
Dim mostCommonPair As String = Nothing
Dim maxCount As Integer = 0
For Each entry In pairCount
If entry.Value > maxCount Then
mostCommonPair = entry.Key
maxCount = entry.Value
End If
Next
Console.WriteLine("Max count: " & maxCount)
Return mostCommonPair
End Function
End Class
' run:
'
' Max count: 7
' The most common pair: xz
'