Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 358
- Office Version
- 2003 or older
- Platform
- Windows
The below code gives the count of repeating substrings. How can I make it write all the substrings that lead to the count?
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.
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: