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 = 3Output:12Explanation:

Possible permutations satisfying the required conditions are {aba, abc, aca, acb, bac, bab, bca, bcb, cac, cab, cba, cbc}

Input:N = 5Output:48

**Approach:**

Following observations need to be made to solve the given problem:

- Let us fix the first letter as
**‘a’**. - Now, the 2
^{nd}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 2
^{nd}position is ‘b’, the third character can either be ‘a’ or ‘c’. If the character in 2^{nd}position is ‘c’, the third character can either be ‘a’ or ‘b’. - Similarly, for all the remaining positions, there will always be two possiblities 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 * 2**.^{N – 1} - Therefore, the total number of permutations, considering the first character can also be ‘b’ or ‘c’ as well, is
**3 * 2**.^{N – 1}

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print the number of ` `// permutations possible ` `int` `countofPermutations(` `int` `N) ` `{ ` ` ` `return` `int` `(3 * ` `pow` `(2, N - 1)); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 5; ` ` ` `cout << countofPermutations(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `class` `GFG{ ` ` ` `// Function to print the number of ` `// permutations possible ` `static` `int` `countofPermutations(` `int` `N) ` `{ ` ` ` `return` `(` `int` `)(` `3` `* Math.pow(` `2` `, N - ` `1` `)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `5` `; ` ` ` `System.out.print(countofPermutations(N)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approach ` ` ` `# Function to print the number of ` `# permutations possible ` `def` `countofPermutations(N): ` ` ` ` ` `return` `int` `((` `3` `*` `pow` `(` `2` `, N ` `-` `1` `))); ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `N ` `=` `5` `; ` ` ` `print` `(countofPermutations(N)); ` ` ` `# This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to print the number of ` `// permutations possible ` `static` `int` `countofPermutations(` `int` `N) ` `{ ` ` ` `return` `(` `int` `)(3 * Math.Pow(2, N - 1)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 5; ` ` ` ` ` `Console.Write(countofPermutations(N)); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

**Output:**

48

**Time Complexity: **O(logN) **Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of distinct permutations of every possible length of given string
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Smallest length string with repeated replacement of two distinct adjacent
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Count of K-size substrings having palindromic permutations
- Count of permutations of an Array having each element as a multiple or a factor of its index
- Min and max length subarray having adjacent element difference atmost K
- Contiguous subsegments of a string having distinct subsequent characters
- Find the String having each substring with exactly K distinct characters
- Reduce string to shortest length by deleting a pair of same adjacent characters
- Generate permutations with only adjacent swaps allowed
- Permutations of string such that no two vowels are adjacent
- Length of longest subarray having only K distinct Prime Numbers
- Print all distinct permutations of a given string with duplicates
- Print first n distinct permutations of string using itertools in Python
- Java Program to print distinct permutations of a string
- Iterative program to generate distinct Permutations of a String
- Distinct permutations of the string | Set 2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.