Generate list of all possible permutations of a string

后端 未结 30 2546
故里飘歌
故里飘歌 2020-11-22 15:10

How would I go about generating a list of all possible permutations of a string between x and y characters in length, containing a variable list of characters.

Any l

相关标签:
30条回答
  • 2020-11-22 15:28

    Though this doesn't answer your question exactly, here's one way to generate every permutation of the letters from a number of strings of the same length: eg, if your words were "coffee", "joomla" and "moodle", you can expect output like "coodle", "joodee", "joffle", etc.

    Basically, the number of combinations is the (number of words) to the power of (number of letters per word). So, choose a random number between 0 and the number of combinations - 1, convert that number to base (number of words), then use each digit of that number as the indicator for which word to take the next letter from.

    eg: in the above example. 3 words, 6 letters = 729 combinations. Choose a random number: 465. Convert to base 3: 122020. Take the first letter from word 1, 2nd from word 2, 3rd from word 2, 4th from word 0... and you get... "joofle".

    If you wanted all the permutations, just loop from 0 to 728. Of course, if you're just choosing one random value, a much simpler less-confusing way would be to loop over the letters. This method lets you avoid recursion, should you want all the permutations, plus it makes you look like you know Maths(tm)!

    If the number of combinations is excessive, you can break it up into a series of smaller words and concatenate them at the end.

    0 讨论(0)
  • 2020-11-22 15:29

    There are several ways to do this. Common methods use recursion, memoization, or dynamic programming. The basic idea is that you produce a list of all strings of length 1, then in each iteration, for all strings produced in the last iteration, add that string concatenated with each character in the string individually. (the variable index in the code below keeps track of the start of the last and the next iteration)

    Some pseudocode:

    list = originalString.split('')
    index = (0,0)
    list = [""]
    for iteration n in 1 to y:
      index = (index[1], len(list))
      for string s in list.subset(index[0] to end):
        for character c in originalString:
          list.add(s + c)
    

    you'd then need to remove all strings less than x in length, they'll be the first (x-1) * len(originalString) entries in the list.

    0 讨论(0)
  • 2020-11-22 15:29

    Here is a simple word C# recursive solution:

    Method:

    public ArrayList CalculateWordPermutations(string[] letters, ArrayList words, int index)
            {
                bool finished = true;
                ArrayList newWords = new ArrayList();
                if (words.Count == 0)
                {
                    foreach (string letter in letters)
                    {
                        words.Add(letter);
                    }
                }
    
                for(int j=index; j<words.Count; j++)
                {
                    string word = (string)words[j];
                    for(int i =0; i<letters.Length; i++)
                    {
                        if(!word.Contains(letters[i]))
                        {
                            finished = false;
                            string newWord = (string)word.Clone();
                            newWord += letters[i];
                            newWords.Add(newWord);
                        }
                    }
                }
    
                foreach (string newWord in newWords)
                {   
                    words.Add(newWord);
                }
    
                if(finished  == false)
                {
                    CalculateWordPermutations(letters, words, words.Count - newWords.Count);
                }
                return words;
            }
    

    Calling:

    string[] letters = new string[]{"a","b","c"};
    ArrayList words = CalculateWordPermutations(letters, new ArrayList(), 0);
    
    0 讨论(0)
  • 2020-11-22 15:30

    Here is a simple solution in C#.

    It generates only the distinct permutations of a given string.

        static public IEnumerable<string> permute(string word)
        {
            if (word.Length > 1)
            {
    
                char character = word[0];
                foreach (string subPermute in permute(word.Substring(1)))
                {
    
                    for (int index = 0; index <= subPermute.Length; index++)
                    {
                        string pre = subPermute.Substring(0, index);
                        string post = subPermute.Substring(index);
    
                        if (post.Contains(character))
                                continue;                       
    
                        yield return pre + character + post;
                    }
    
                }
            }
            else
            {
                yield return word;
            }
        }
    
    0 讨论(0)
  • 2020-11-22 15:30

    In ruby:

    str = "a"
    100_000_000.times {puts str.next!}
    

    It is quite fast, but it is going to take some time =). Of course, you can start at "aaaaaaaa" if the short strings aren't interesting to you.

    I might have misinterpreted the actual question though - in one of the posts it sounded as if you just needed a bruteforce library of strings, but in the main question it sounds like you need to permutate a particular string.

    Your problem is somewhat similar to this one: http://beust.com/weblog/archives/000491.html (list all integers in which none of the digits repeat themselves, which resulted in a whole lot of languages solving it, with the ocaml guy using permutations, and some java guy using yet another solution).

    0 讨论(0)
  • 2020-11-22 15:31

    I needed this today, and although the answers already given pointed me in the right direction, they weren't quite what I wanted.

    Here's an implementation using Heap's method. The length of the array must be at least 3 and for practical considerations not be bigger than 10 or so, depending on what you want to do, patience and clock speed.

    Before you enter your loop, initialise Perm(1 To N) with the first permutation, Stack(3 To N) with zeroes*, and Level with 2**. At the end of the loop call NextPerm, which will return false when we're done.

    * VB will do that for you.

    ** You can change NextPerm a little to make this unnecessary, but it's clearer like this.

    Option Explicit
    
    Function NextPerm(Perm() As Long, Stack() As Long, Level As Long) As Boolean
    Dim N As Long
    If Level = 2 Then
        Swap Perm(1), Perm(2)
        Level = 3
    Else
        While Stack(Level) = Level - 1
            Stack(Level) = 0
            If Level = UBound(Stack) Then Exit Function
            Level = Level + 1
        Wend
        Stack(Level) = Stack(Level) + 1
        If Level And 1 Then N = 1 Else N = Stack(Level)
        Swap Perm(N), Perm(Level)
        Level = 2
    End If
    NextPerm = True
    End Function
    
    Sub Swap(A As Long, B As Long)
    A = A Xor B
    B = A Xor B
    A = A Xor B
    End Sub
    
    'This is just for testing.
    Private Sub Form_Paint()
    Const Max = 8
    Dim A(1 To Max) As Long, I As Long
    Dim S(3 To Max) As Long, J As Long
    Dim Test As New Collection, T As String
    For I = 1 To UBound(A)
        A(I) = I
    Next
    Cls
    ScaleLeft = 0
    J = 2
    Do
        If CurrentY + TextHeight("0") > ScaleHeight Then
            ScaleLeft = ScaleLeft - TextWidth(" 0 ") * (UBound(A) + 1)
            CurrentY = 0
            CurrentX = 0
        End If
        T = vbNullString
        For I = 1 To UBound(A)
            Print A(I);
            T = T & Hex(A(I))
        Next
        Print
        Test.Add Null, T
    Loop While NextPerm(A, S, J)
    J = 1
    For I = 2 To UBound(A)
        J = J * I
    Next
    If J <> Test.Count Then Stop
    End Sub
    

    Other methods are described by various authors. Knuth describes two, one gives lexical order, but is complex and slow, the other is known as the method of plain changes. Jie Gao and Dianjun Wang also wrote an interesting paper.

    0 讨论(0)
提交回复
热议问题