How to check if a string can be constructed by using the letters of another string in VB.NET

1 Answer

0 votes
Imports System

Module Program

    Function CanConstruct(str As String, anotherStr As String) As Boolean
        Dim freq(255) As Integer   ' ASCII frequency table

        ' Count characters in anotherStr
        For Each ch As Char In anotherStr
            freq(Convert.ToInt32(ch)) += 1
        Next

        ' Check if str can be constructed
        For Each ch As Char In str
            freq(Convert.ToInt32(ch)) -= 1
            If freq(Convert.ToInt32(ch)) < 0 Then
                Return False
            End If
        Next

        Return True
    End Function

    Sub Main()
        Dim str As String = "hello"
        Dim anotherStr As String = "olehhlxyz"

        Console.WriteLine(CanConstruct(str, anotherStr).ToString().ToLower())
    End Sub

End Module


' run:
'
' true
'

 



answered Jan 5 by avibootz

Related questions

...