Results 1 to 6 of 6

Thread: Anagram Algortihm

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Anagram Algortihm

    Ok, I have scoured the internet for days and even checked these forums. I do have an idea of how the algorithm should work, I just have no idea how to implement it.

    Say you have the word ABCDEF.

    The way I figure to solve it is to split it into columns, per se.
    |A|B|C|D|E|F|



    So the first iteration would yield A, B, C, D, E, F. Now, using each of those in the first column, increment the second column.

    AB
    AC
    AD
    AE
    AF

    Increment the third column.

    ABC
    ABD
    ABE
    ABF

    Since we have reached the last letter in the third column, back up and increment the previous column.

    ACD
    ACE
    ACF

    Notice I don't use ACB, as that is exactly the same as ABC in terms of being unique.
    Since the last letter was used, increment the previous column again.

    ADE
    ADF <- Last letter reached, increment previous
    AEF

    That is all the UNIQUE three letter combinations possible. Now, step up to four letters

    ABCD
    ABCE
    ABCF <- Last letter reached, increment previous
    ABDE
    ABDF <- Last letter reached, increment previous
    ABEF

    The third column cannot be F because that is the last letter and would only allow a three letter word, which we have done already. Back up another column.

    ACDE
    ACDF <- Last letter reached, increment previous
    ACEF <- Last letter reached, increment previous
    ADEF <- Last letter reached, increment previous

    Again, the column cannot be D as it would end up a three letter word. That is all the four letter UNIQUE combinations.

    ABCDE
    ABCDF <- Last letter reached, increment previous
    ABCEF <- Last letter reached, increment previous
    ABDEF

    That is all the five letter combinations. And then, of course, the only 6 letter combination, ABCDEF.

    Now, here comes the fun part. Notice how all the above combination start with the first letter. To get a whole list starting with the second letter, hack off the first.

    Example: ABCDE -> BCDE -> CDE -> DE -> D

    Now that I have shown the method, I need help with the implementation.
    Note that the word may have more than one of the same letter (e.g AABCDE) so I would need to keep track of the letter that I am on.

    Say I just finished ABF and am heading to ACD. I need to know that I am changing the second column from the second letter to the third letter. That means that the third column cannot be any letter less than the fourth letter.

    Thanks for helping.
    Prefix has no suffix, but suffix has a prefix.

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Anagram Algortihm

    I don't really understand that, but here's something for solving anagrams: (see comments at bottom)
    Code:
    Dim lettersgiven() As Char = {"a"c,"b"c,"d"c,"e"c,"f"c}
    Dim results As New List(Of String)
    For start As Integer = 0 To lettersgiven.Length - 1
         Dim usedindices As New List(Of Integer)
         For j As Integer = 1 To lettersgiven.Length
              Dim i As Integer = start
              While usedindices.Contains(i)
                   i = (i + 1) Mod lettersgiven.Length
              End While
              usedindices.Add(i)
         Next
         Dim sb As New System.Text.StringBuilder()
         For Each i As Integer In usedindices
              sb.Append(lettersgiven(i))
         Next
         If LookupDictionary(sb.ToString()) Then results.Add(sb.ToString())
    Next
    'Now, results is a list of all the possible words.
    'You'll need to provide a "LookupDictionary" method
    'that returns True when it's a real word.

  3. #3

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: Anagram Algortihm

    All that code did was take the first letter and stick it on the end.

    So, with input of 'abcdef' I got:
    bcdefa, cdefab, defabc, efabcd, fabcde
    Prefix has no suffix, but suffix has a prefix.

  4. #4

  5. #5

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: Anagram Algortihm

    I am trying to get all possible UNIQUE letter combinations of a specific string.
    Retain and retina are not unique because they have the same letters.

    I am thinking of using an integer array that will hold the current letter in a certain position.
    If the input string is 'abcdef' then an array like so {2, 3, 4} would be 'cde'.
    Basically, each index in the array is a column of the string (explained in the first post) and the value of that index is the actual letter.

    In the example {2, 3, 4}; c is the first letter because the first index contains a 2. c is in index 2 of the input string.
    I am still fiddling around. Thanks for the input thus far.
    Last edited by Troy Lundin; May 9th, 2010 at 06:34 PM.
    Prefix has no suffix, but suffix has a prefix.

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Anagram Algortihm

    Quote Originally Posted by Troy Lundin View Post
    I am trying to get all possible UNIQUE letter combinations of a specific string.
    That is what you should have started in the first place.

    Check my signature for Unique Combinations.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width