?
Solved

Get all combinations of recursive substring replacement

Posted on 2012-08-30
4
Medium Priority
?
486 Views
Last Modified: 2012-09-05
Hello!
I'm looking for a way to replace a substring within a string, and obtaining all different combinations, for example:

"one leg two legs", and replacing "leg" with "arm" it would return the following strings:

"one leg two legs"
"one arm two legs"
"one leg two arms"
"one arm two arms"

I have made a quite simple routine for this but it fails when replacing "i" with "ie" (infinite recursion because string builds up, pie > piee > pieee > pieeee) and if i fix that by recursing backwards if tails when replacing for example "ch" with "sch" (michael > mischael > misschael > missschael) - im just stuck as hell and trying all night - please help?
0
Comment
Question by:MisterMiyagi
  • 2
4 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38351803
When you replace a string, advance the pointer the length of the string you just replaced with.

What functions are you using? Find? The parameter for find that tells where to start the search would be the pointer. So just increment that by WordYouJustAdded.Length
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 38352996
Try something like this out:
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim data As String = "one leg two legs three leg eight legs"
        Dim find As String = "leg"
        Dim replace As String = "arm"

        Dim values() As String = data.Split(New String() {find}, StringSplitOptions.None)
        Dim combos As Integer = Math.Pow(2, values.Length - 1)

        Dim binary As String
        Dim maxWidth As Integer = Convert.ToString(combos - 1, 2).Length
        Dim combination As New System.Text.StringBuilder
        For i As Integer = 0 To combos - 1
            binary = Convert.ToString(i, 2).PadLeft(maxWidth, "0")
            combination.Clear()
            For j = 0 To values.Length - 2
                combination.Append(values(j) & IIf(binary.Substring(j, 1) = "0", find, replace))
            Next
            combination.Append(values(values.GetUpperBound(0)))
            Debug.Print(combination.ToString)
        Next
    End Sub

End Class

Open in new window


Which produced the following output:
one leg two legs three leg eight legs
one leg two legs three leg eight arms
one leg two legs three arm eight legs
one leg two legs three arm eight arms
one leg two arms three leg eight legs
one leg two arms three leg eight arms
one leg two arms three arm eight legs
one leg two arms three arm eight arms
one arm two legs three leg eight legs
one arm two legs three leg eight arms
one arm two legs three arm eight legs
one arm two legs three arm eight arms
one arm two arms three leg eight legs
one arm two arms three leg eight arms
one arm two arms three arm eight legs
one arm two arms three arm eight arms
0
 

Author Closing Comment

by:MisterMiyagi
ID: 38370609
Thx mate!
0
 

Author Comment

by:MisterMiyagi
ID: 38370611
Thank you TommySzalapski! Im sure your code would have worked too, however I preferred the second one since it didnt use recursing (brute) but mathematically found all combinations (finesse), thanks a lot for your help though!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question