Permutation of an array, with repetition, in Java

前端 未结 4 1056
名媛妹妹
名媛妹妹 2021-01-12 01:47

There are some similar questions on the site that have been of some help, but I can\'t quite nail down this problem, so I hope this is not repetitive.

This is a home

相关标签:
4条回答
  • 2021-01-12 02:12

    Here is c# version to generate the permutations of given string with repetitions:

    (essential idea is - number of permutations of string of length 'n' with repetitions is n^n).

    string[] GetPermutationsWithRepetition(string s)
            {
                s.ThrowIfNullOrWhiteSpace("s");
                List<string> permutations = new List<string>();
                this.GetPermutationsWithRepetitionRecursive(s, "",
                    permutations);
                return permutations.ToArray();
            }
            void GetPermutationsWithRepetitionRecursive(string s, string permutation, List<string> permutations)
            {
                if(permutation.Length == s.Length)
                {
                    permutations.Add(permutation);
                    return;
                }
                for(int i =0;i<s.Length;i++)
                {
                    this.GetPermutationsWithRepetitionRecursive(s, permutation + s[i], permutations);
                }
            }
    

    Below are the corresponding unit tests:

    [TestMethod]
            public void PermutationsWithRepetitionTests()
            {
                string s = "";
                int[] output = { 1, 4, 27, 256, 3125 };
                for(int i = 1; i<=5;i++)
                {
                    s += i;
                    var p = this.GetPermutationsWithRepetition(s);
                    Assert.AreEqual(output[i - 1], p.Length);
                }
            }
    
    0 讨论(0)
  • 2021-01-12 02:14

    I just had an idea. What if you added a hidden character (H for hidden) [A, B, C, H], then did all the fixed length permutations of it (you said you know how to do that). Then when you read it off, you stop at the hidden character, e.g. [B,A,H,C] would become (B,A).

    Hmm, the downside is that you would have to track which ones you created though [B,H,A,C] is the same as [B,H,C,A]

    0 讨论(0)
  • 2021-01-12 02:19

    If I understand correctly, you are given a set of characters c and the desired length n.

    Technically, there's no such thing as a permutation with repetition. I assume you want all strings of length n with letters from c.

    You can do it this way:

    to generate all strings of length N with letters from C
     -generate all strings of length N with letters from C
         that start with the empty string.
    
    to generate all strings of length N with letters from C
       that start with a string S
     -if the length of S is N
      -print S
     -else for each c in C
      -generate all strings of length N with letters from C that start with S+c
    

    In code:

    printAll(char[] c, int n, String start){
      if(start.length >= n){
        System.out.println(start)
      }else{
        for(char x in c){ // not a valid syntax in Java
          printAll(c, n, start+x);
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-12 02:20

    I use this java realization of permutations with repetitions. A~(n,m): n = length of array, m = k. m can be greater or lesser then n.

    public class Permutations {
    
    
        static void permute(Object[] a, int k, PermuteCallback callback) {
            int n = a.length;
    
            int[] indexes = new int[k];
            int total = (int) Math.pow(n, k);
    
            Object[] snapshot = new Object[k];
            while (total-- > 0) {
                for (int i = 0; i < k; i++){
                    snapshot[i] = a[indexes[i]];
                }
                callback.handle(snapshot);
    
                for (int i = 0; i < k; i++) {
                    if (indexes[i] >= n - 1) {
                        indexes[i] = 0;
                    } else {
                        indexes[i]++;
                        break;
                    }
                }
            }
        }
    
        public static interface PermuteCallback{
            public void handle(Object[] snapshot);
        };
    
        public static void main(String[] args) {
            Object[] chars = { 'a', 'b', 'c', 'd' };
            PermuteCallback callback = new PermuteCallback() {
    
                @Override
                public void handle(Object[] snapshot) {
                    for(int i = 0; i < snapshot.length; i ++){
                        System.out.print(snapshot[i]);
                    }
                    System.out.println();
                }
            };
            permute(chars, 8, callback);
        }
    
    }
    

    Example output is

    aaaaaaaa
    baaaaaaa
    caaaaaaa
    daaaaaaa
    abaaaaaa
    bbaaaaaa
    ...
    bcffffdffffd
    ccffffdffffd
    dcffffdffffd
    affffdffffdd
    bffffdffffdd
    cffffdffffdd
    ffffdffffffffd
    
    0 讨论(0)
提交回复
热议问题