Repeating Substrings

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
358
Office Version
  1. 2003 or older
Platform
  1. Windows
The below code gives the count of repeating substrings. How can I make it write all the substrings that lead to the count?

VBA Code:
Function NumDistinct(s As String, t As String) As Long
    Dim sLen As Long
    Dim tLen As Long
    Dim i As Long
    Dim j As Long
   
    sLen = Len(s)
    tLen = Len(t)
   
    Dim dp() As Variant
    ReDim dp(0 To sLen, 0 To tLen) As Variant
   
    dp(0, 0) = 1
   
    ' Initialize first column of dp array
    For i = 1 To sLen
        dp(i, 0) = 1
    Next i
   
    ' Initialize first row of dp array
    For j = 1 To tLen
        dp(0, j) = 0
    Next j
   
    ' Fill the dp array using dynamic programming approach
    For i = 1 To sLen
        For j = 1 To tLen
            dp(i, j) = dp(i - 1, j)
            If Mid(s, i, 1) = Mid(t, j, 1) Then
                dp(i, j) = dp(i, j) + dp(i - 1, j - 1)
            End If
        Next j
    Next i
   
    ' Return the value at dp(sLen, tLen)
    NumDistinct = dp(sLen, tLen)
End Function

When s = "babbbit" and t = "bit", the count output is 4. And these 4 subsequences are derived from the following indices of s:s[0]s[5]s[6] = bit, s[2]s[5]s[6] = bit, s[3]s[5]s[6] = bit, s[4]s[5]s[6] = bit. I want the code to give these indices.
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
The below code gives the count of repeating substrings

When s = "babbbit" and t = "bit", the count output is 4.

I don't understand what your function is intended to do. If the purpose of the function is to count repeating substrings, and if the substring for the example is "bit", I only see one instance of "bit" in the main string ("babbbit"), not four instances.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top