Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,999 questions

51,944 answers

573 users

How to count the characters need to be removed so that two strings become anagram in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Public Shared Function CountCharactersNeedToBeRemovedForAnagram(ByVal str1 As String, ByVal str2 As String) As Integer
        Dim TotalABCLetters As Integer = 26
        Dim count1 As Integer() = New Integer(TotalABCLetters - 1) {}
        Dim count2 As Integer() = New Integer(TotalABCLetters - 1) {}
        Dim size1 As Integer = str1.Length
        Dim size2 As Integer = str2.Length

        For i As Integer = 0 To size1 - 1
			count1(Convert.ToInt32(str1(i)) - 97) += 1 ' "a"c = 97 ASCII
        Next

        For i As Integer = 0 To size2 - 1
			count2(Convert.ToInt32(str2(i)) - Convert.ToByte("a"c)) += 1
       Next

        Dim result As Integer = 0

        For i As Integer = 0 To TotalABCLetters - 1
            result += Math.Abs(count1(i) - count2(i))
        Next

        Return result
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim str1 As String = "masterfx"
        Dim str2 As String = "ksampret"

        Console.Write(CountCharactersNeedToBeRemovedForAnagram(str1, str2))
    End Sub
End Class




' run:
'
' 4
'

 



answered Sep 28, 2022 by avibootz
...