John Roberts
John Roberts

Reputation: 5966

Count All Permutations Where No Two Adjacent Characters Are the Same

Given a sequence which contains only various amounts of the numbers 1, 2, 3, and 4 (examples: 13244, 4442, etc), I want to count all its permutations such that no two adjacent numbers are the same. I believe it is O(N! * N) and want to know if there is a better one out there. Anyone have any ideas?

 class Ideone
    {
        static int permutationCount++;
        public static void main(String[] args) {
            String str = "442213";
            permutation("", str);
            System.out.println(permutationCount);
        }

        private static void permutation(String prefix, String str) {
            int n = str.length();
            if (n == 0){
                boolean bad = false;
                //Check whether there are repeating adjacent characters
                for(int i = 0; i < prefix.length()-1; i++){
                    if(prefix.charAt(i)==prefix.charAt(i+1))
                        bad = true;
                }
                if(!bad){
                    permutationCount++;
                }
            }
            else {
                //Recurse through permutations
                for (int i = 0; i < n; i++)
                    permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
            }
        }
    }

Upvotes: 4

Views: 5600

Answers (3)

jakub.petr
jakub.petr

Reputation: 3031

I understand your question like this: Given a string containing only numbers 1,2,3,4 - how many permutations of these characters exist that when you put them into string again there won't be any same adjecent numbers.

I would suggest this approach:

  L - length of your string
  n1 - how many times is 1 repeated, n2 - how many times is 2 repeated etc.

  P - number of all possible permutations
  P = L! / (n1!*n2!*n3!*n4!)

  C - number of all solutions fitting your constraint
  C = P - start with all permutations

  substract all permutations which have 11 in it (just take 11 as one number)
  C = C - (L - 1)! / ((n1 - 1)! * n2! * n3! * n4!)

  ... do the same for 22 ...

  add all permutations which have both 11 and 22 in it (because we have substracted them twice, so you need to add them)
  C = C + (L - 2)! / ((n1 - 1)! * (n2 - 1)! * n3! * n4!)

  ... repeat previous steps for 33 and 44 ...

Upvotes: 2

LucasP
LucasP

Reputation: 1715

Edit: After your edits and comments it is clear that I misunderstood the question.

If you just want to check to see if there are no matching adjacent numbers, then you can use a simple loop. This will be O(n) complexity.

public static void main(String[] args) {
    String str = "442213";
    System.out.println(permutation(str));
}

public static int permutation(String str) {
    int permutationCount = 0;
    if (str.length() > 1) {
        for (int i = 0; i < str.length() - 1; i++) {
            if (str.charAt(i) != str.charAt(i + 1)) {
                permutationCount++;
            }
        }
    }
    return permutationCount;
}

If you wanted to stick with recursion, you could do something like this:

public static void main(String[] args) {
    String str = "442213";
    System.out.println(permutation(str, 0));
}

public static int permutation(String str, int currentIndex) {
    int permutationCount = 0;
    if (str == null || currentIndex + 1 >= str.length()) {
        return permutationCount;
    }

    if (str.charAt(currentIndex) != str.charAt(currentIndex + 1)) {
        permutationCount = 1;
    }

    return permutationCount + permutation(str, currentIndex + 1);
}

Upvotes: 0

dognose
dognose

Reputation: 20889

If you just want to calculate how many permutations match your constraint, it is not required to spell each of them out.

If I get your question right, your input string has 4 distinct input characters 1,2,3,4 and you want to know how many permutations of this are possible?

Then you should use some maths, namely n! / (n-r)!, where n is the number of elements to choose from (4 in this case) and r is the number of positions you want to fill (also 4).

Your example would have 4! / (4-4)! = 24 permutations possible:

{1,2,3,4} {1,2,4,3} {1,3,2,4} {1,3,4,2} {1,4,2,3} {1,4,3,2} 
{2,1,3,4} {2,1,4,3} {2,3,1,4} {2,3,4,1} {2,4,1,3} {2,4,3,1} 
{3,1,2,4} {3,1,4,2} {3,2,1,4} {3,2,4,1} {3,4,1,2} {3,4,2,1} 
{4,1,2,3} {4,1,3,2} {4,2,1,3} {4,2,3,1} {4,3,1,2} {4,3,2,1}

In a nutshell, for length n with n distinct values the count of permutations is n!:

1 -> 1
2 -> 2
3 -> 6
4 -> 24
5 -> 120
...

Upvotes: 0

Related Questions