Count of distinct permutations of length N having no similar adjacent characters - GeeksforGeeks

29 Apr, 2021
Given an integer N, the task is to calculate the total number of distinct permutations of length N, consisting only of letters ‘a’, ‘b’, and ‘c’, with repetitions allowed, such that no two adjacent characters are the same.
Input: N = 3 Output: 12 Explanation: Possible permutations satisfying the required conditions are {aba, abc, aca, acb, bac, bab, bca, bcb, cac, cab, cba, cbc}Input: N = 5 Output: 48
Approach: Following observations need to be made to solve the given problem:  
Let us fix the first letter as ‘a’.Now, the 2nd letter can either be ‘b’ or ‘c’, which leaves 2 ways to fill the second letter.Similarly, the third letter can also be filled in two ways. If the character in 2nd position is ‘b’, the third character can either be ‘a’ or ‘c’. If the character in 2nd position is ‘c’, the third character can either be ‘a’ or ‘b’.Similarly, for all the remaining positions, there will always be two possibilities depending on the character in the previous position. Therefore, the total number of possible permutation if ‘a’ occupies the first position is 1*2*2*2...*2 = 1 * 2N – 1.Therefore, the total number of permutations, considering the first character can also be ‘b’ or ‘c’ as well, is 3 * 2N – 1.
Let us fix the first letter as ‘a’.
Now, the 2nd letter can either be ‘b’ or ‘c’, which leaves 2 ways to fill the second letter.
Similarly, the third letter can also be filled in two ways. If the character in 2nd position is ‘b’, the third character can either be ‘a’ or ‘c’. If the character in 2nd position is ‘c’, the third character can either be ‘a’ or ‘b’.
Similarly, for all the remaining positions, there will always be two possibilities depending on the character in the previous position. Therefore, the total number of possible permutation if ‘a’ occupies the first position is 1*2*2*2...*2 = 1 * 2N – 1.
Therefore, the total number of permutations, considering the first character can also be ‘b’ or ‘c’ as well, is 3 * 2N – 1.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ Program to implement// the above approach#include <bits/stdc++.h>using namespace std; // Function to print the number of// permutations possibleint countofPermutations(int N){    return int(3 * pow(2, N - 1));} // Driver Codeint main(){    int N = 5;    cout << countofPermutations(N);    return 0;}
// Java program to implement// the above approachclass GFG{ // Function to print the number of// permutations possiblestatic int countofPermutations(int N){    return (int)(3 * Math.pow(2, N - 1));} // Driver Codepublic static void main(String[] args){    int N = 5;    System.out.print(countofPermutations(N));}} // This code is contributed by 29AjayKumar
# Python3 program to implement# the above approach # Function to print the number of# permutations possibledef countofPermutations(N):         return int((3 * pow(2, N - 1))); # Driver Codeif __name__ == '__main__':         N = 5;    print(countofPermutations(N)); # This code is contributed by amal kumar choubey
// C# program to implement// the above approachusing System; class GFG{ // Function to print the number of// permutations possiblestatic int countofPermutations(int N){    return (int)(3 * Math.Pow(2, N - 1));} // Driver Codepublic static void Main(String[] args){    int N = 5;         Console.Write(countofPermutations(N));}} // This code is contributed by Amit Katiyar
<script> // Javascript Program to implement// the above approach // Function to print the number of// permutations possiblefunction countofPermutations(N){    return parseInt(3 * Math.pow(2, N - 1));} // Driver Codevar N = 5;document.write( countofPermutations(N)); </script>
48
 
Time Complexity: O(logN) Auxiliary Space: O(1)
29AjayKumar
amit143katiyar
Amal Kumar Choubey
rutvik_56
Permutation and Combination
Combinatorial
Mathematical
School Programming
Mathematical
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Combinations with repetitions
Ways to sum to N using Natural Numbers up to K with repetitions allowed
Largest number by rearranging digits of a given positive or negative number
Given number of matches played, find number of teams in tournament
Generate all possible combinations of at most X characters from a given array
Program for Fibonacci numbers
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7
Program to find GCD or HCF of two numbers