Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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: 
 

  1. Let us fix the first letter as ‘a’.
  2. Now, the 2nd letter can either be ‘b’ or ‘c’, which leaves 2 ways to fill the second letter.
  3. 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’.
  4. 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.
  5. 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++




// 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;
}


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


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


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


Javascript




<script>
 
// Javascript Program to implement
// the above approach
 
// Function to print the number of
// permutations possible
function countofPermutations(N)
{
    return parseInt(3 * Math.pow(2, N - 1));
}
 
// Driver Code
var N = 5;
document.write( countofPermutations(N));
 
</script>


Output: 

48

 

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



Last Updated : 29 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads