View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Andrew Taylor Andrew Taylor is offline
external usenet poster
 
Posts: 225
Default Pattern / Combinations

A simple recursive approach seems to work quite well (though
it doesn't give them in alphabetical order):

Option Explicit
Sub main()
ShowCombinations "", "ABCDE"
End Sub

Sub ShowCombinations(strPrefix As String, strMain As String)
If strMain = "" Then
Debug.Print strPrefix
Exit Sub
End If
Dim strFirst As String, strRest As String
strFirst = Left(strMain, 1)
strRest = Mid(strMain, 2)
ShowCombinations strPrefix & strFirst, strRest
ShowCombinations strPrefix, strRest
End Sub



Paul W Smith wrote:
Does anyone know of any algorithms which produce all the possible
combinations of patterns within a string?

Example String = ABCDE

AB, AC, AD, AE, BC, BD, BE, CD, CE, DE
ABC, ABD, ABE, ACD, ACE, ADE, BCD, BCE, CDE
ABCD, ABCE, ABDE, ACDE, BDCE
ABCDE