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,943 questions

51,883 answers

573 users

How to find all occurrences of all substring permutations (anagrams) in a string with VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic
 
Public Class Program
    Private Const SIZE As Integer = 256
 
    Public Shared Function Compare(ByVal arr_str As Byte(), ByVal arr_sub As Byte()) As Boolean
        For i As Integer = 0 To SIZE - 1
            If arr_str(i) <> arr_sub(i) Then Return False
        Next
 
        Return True
    End Function
 
    Public Shared Function find_substring_permutations(ByVal str As String, ByVal substr As String) As List(Of Integer)
        Dim result_list As List(Of Integer) = New List(Of Integer)()
        Dim countSub As Byte() = New Byte(255) {}
        Dim countStr As Byte() = New Byte(255) {}
 
        For i As Integer = 0 To substr.Length - 1
            countSub(Asc(substr(i))) = countSub(Asc(substr(i))) + 1
            countStr(Asc(str(i))) = countStr(Asc(str(i))) + 1
        Next
 
        For i As Integer = substr.Length To str.Length - 1
            If Compare(countSub, countStr) Then result_list.Add(i - substr.Length)
            countStr(Asc(str(i))) = countStr(Asc(str(i))) + 1
            countStr(Asc(str(i - substr.Length))) = countStr(Asc(str(i - substr.Length))) - 1
        Next
 
        If Compare(countSub, countStr) Then result_list.Add(str.Length - substr.Length)
         
        Return result_list
    End Function
 
    Public Shared Sub Main()
        '  0 CBA : 5 BAC : 11 ABC : 12 BCA : 13 CAB : 21 ACB : 26 CAB
        Dim s As String = "CBAxyBACdarABCABbcapoACBqtCAB"
        Dim substr As String = "ABC"
                                         
        Dim result As List(Of Integer) = find_substring_permutations(s, substr)
                                             
        For Each n As Integer In result
            Console.Write(n & " ")
        Next
    End Sub
End Class
 
 
 
 
' run
'
' 0 5 11 12 13 21 26 
'

 



answered Jan 2, 2022 by avibootz
edited Jan 2, 2022 by avibootz
...