Open In App

Count of palindromic strings of size upto N consisting of first K alphabets occurring at most twice

Given two integers N and K, the task is to find the number of palindromic strings of size at most N consisting of the first K lowercase alphabets such that each character in a string doesn’t appear more than twice.

Examples:



Input: N = 3, K = 2
Output: 6
Explanation:
The possible strings are:
“a”, “b”, “aa”, “bb”, “aba”, “bab”.

Input: N = 4, K = 3
Output: 18
Explanation:
The possible strings are: 
“a”, “b”, “c”, “aa”, “bb”, “cc”, “aba”, “aca”, “bab”, “bcb”, “cac”, “cbc”,  “abba”, “acca”, “baab”, “bccb”, “caac”, “cbbc”.



Approach: The given problem can be solved based on the following observations:

Follow the steps below to solve the given problem:

  1. For finding the count palindromic strings of length at most N, then count palindromic strings of each length from 1 to N and then add them together.
  2. For the value of N as:
    • If N is even, then find all combinations possible till N/2 because only half of the positions can be filled.
    • If N is odd, then find all combinations possible till N/2 +1, and extra + 1 for the element as the middle element.
  3. Add all of them together and print the answer accordingly.

Below is the implementation of the above approach:




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function of return the number of
// palindromic strings of length N with
// first K alphabets possible
int lengthNPalindrome(int N, int K)
{
    int half = N / 2;
 
    // If N is odd, half + 1 position
    // can be filled to cope with the
    // extra middle element
    if (N & 1) {
        half += 1;
    }
 
    int ans = 1;
    for (int i = 1; i <= half; i++) {
        ans *= K;
 
        // K is reduced by one, because
        // count of choices for the next
        // position is  reduced by 1 as
        // a element can only once
        K--;
    }
 
    // Return the possible count
    return ans;
}
 
// Function to find the count of palindromic
// string of first K characters according
// to the given criteria
int palindromicStrings(int N, int K)
{
    // If N=1, then only K palindromic
    // strings possible.
    if (N == 1) {
        return K;
    }
 
    // If N=2, the 2*K palindromic strings
    // possible, K for N=1 and K for N=2
    if (N == 2) {
        return 2 * K;
    }
 
    int ans = 0;
 
    // Initialize ans with the count of
    // strings possible till N = 2
    ans += (2 * K);
 
    for (int i = 3; i <= N; i++) {
        ans += lengthNPalindrome(i, K);
    }
 
    // Return the possible count
    return ans;
}
 
// Driver Code
int main()
{
    int N = 4, K = 3;
    cout << palindromicStrings(N, K);
 
    return 0;
}




// Java program for the above approach
import java.io.*;
 
class GFG {
    // Function of return the number of
    // palindromic strings of length N with
    // first K alphabets possible
    static int lengthNPalindrome(int N, int K)
    {
        int half = N / 2;
 
        // If N is odd, half + 1 position
        // can be filled to cope with the
        // extra middle element
        if (N % 2 == 1) {
            half += 1;
        }
 
        int ans = 1;
        for (int i = 1; i <= half; i++) {
            ans *= K;
 
            // K is reduced by one, because
            // count of choices for the next
            // position is  reduced by 1 as
            // a element can only once
            K--;
        }
 
        // Return the possible count
        return ans;
    }
 
    // Function to find the count of palindromic
    // string of first K characters according
    // to the given criteria
    static int palindromicStrings(int N, int K)
    {
        // If N=1, then only K palindromic
        // strings possible.
        if (N == 1) {
            return K;
        }
 
        // If N=2, the 2*K palindromic strings
        // possible, K for N=1 and K for N=2
        if (N == 2) {
            return 2 * K;
        }
 
        int ans = 0;
 
        // Initialize ans with the count of
        // strings possible till N = 2
        ans += (2 * K);
 
        for (int i = 3; i <= N; i++) {
            ans += lengthNPalindrome(i, K);
        }
 
        // Return the possible count
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 4, K = 3;
 
        System.out.println(palindromicStrings(N, K));
    }
}
// This code is contributed by Potta Lokesh




# Python3 program for the above approach
 
 
# Function of return the number of
# palindromic strings of length N with
# first K alphabets possible
def lengthNPalindrome(N, K) :
     
    half = N // 2;
 
    # If N is odd, half + 1 position
    # can be filled to cope with the
    # extra middle element
    if (N & 1) :
        half += 1;
 
    ans = 1;
    for i in range(1, half + 1) :
        ans *= K;
 
        # K is reduced by one, because
        # count of choices for the next
        # position is  reduced by 1 as
        # a element can only once
        K -= 1;
 
    # Return the possible count
    return ans;
 
# Function to find the count of palindromic
# string of first K characters according
# to the given criteria
def palindromicStrings(N, K) :
 
    # If N=1, then only K palindromic
    # strings possible.
    if (N == 1) :
        return K;
 
    # If N=2, the 2*K palindromic strings
    # possible, K for N=1 and K for N=2
    if (N == 2) :
        return 2 * K;
 
    ans = 0;
 
    # Initialize ans with the count of
    # strings possible till N = 2
    ans += (2 * K);
 
    for i in range(3, N + 1) :
        ans += lengthNPalindrome(i, K);
 
    # Return the possible count
    return ans;
 
# Driver Code
if __name__ == "__main__" :
 
    N = 4; K = 3;
    print(palindromicStrings(N, K));
 
    # This code is contributed by AnkThon




// C# program for the above approach
using System;
 
class GFG
{
   
    // Function of return the number of
    // palindromic strings of length N with
    // first K alphabets possible
    static int lengthNPalindrome(int N, int K)
    {
        int half = N / 2;
 
        // If N is odd, half + 1 position
        // can be filled to cope with the
        // extra middle element
        if (N % 2 == 1) {
            half += 1;
        }
 
        int ans = 1;
        for (int i = 1; i <= half; i++) {
            ans *= K;
 
            // K is reduced by one, because
            // count of choices for the next
            // position is  reduced by 1 as
            // a element can only once
            K--;
        }
 
        // Return the possible count
        return ans;
    }
 
    // Function to find the count of palindromic
    // string of first K characters according
    // to the given criteria
    static int palindromicStrings(int N, int K)
    {
        // If N=1, then only K palindromic
        // strings possible.
        if (N == 1) {
            return K;
        }
 
        // If N=2, the 2*K palindromic strings
        // possible, K for N=1 and K for N=2
        if (N == 2) {
            return 2 * K;
        }
 
        int ans = 0;
 
        // Initialize ans with the count of
        // strings possible till N = 2
        ans += (2 * K);
 
        for (int i = 3; i <= N; i++) {
            ans += lengthNPalindrome(i, K);
        }
 
        // Return the possible count
        return ans;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int N = 4, K = 3;
 
        Console.Write(palindromicStrings(N, K));
    }
}
 
// This code is contributed by shivanisinghss2110




<script>
 
// Javascript program for the above approach
 
// Function of return the number of
// palindromic strings of length N with
// first K alphabets possible
function lengthNPalindrome(N,K)
{
    var half = N / 2;
 
    // If N is odd, half + 1 position
    // can be filled to cope with the
    // extra middle element
    if (N & 1) {
        half += 1;
    }
 
    var ans = 1;
    var i;
    for(i = 1; i <= half; i++) {
        ans *= K;
 
        // K is reduced by one, because
        // count of choices for the next
        // position is  reduced by 1 as
        // a element can only once
        K--;
    }
 
    // Return the possible count
    return ans;
}
 
// Function to find the count of palindromic
// string of first K characters according
// to the given criteria
function palindromicStrings(N, K)
{
    // If N=1, then only K palindromic
    // strings possible.
    if (N == 1) {
        return K;
    }
 
    // If N=2, the 2*K palindromic strings
    // possible, K for N=1 and K for N=2
    if (N == 2) {
        return 2 * K;
    }
 
    ans = 0;
 
    // Initialize ans with the count of
    // strings possible till N = 2
    ans += (2 * K);
 
    for (i = 3; i <= N; i++) {
        ans += lengthNPalindrome(i, K);
    }
 
    // Return the possible count
    return ans;
}
 
// Driver Code
    var N = 4, K = 3;
    document.write(palindromicStrings(N, K));
 
// This code is contributed by ipg2016107.
</script>

 
 

Output: 
18

 

 

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

 


Article Tags :