
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find number of ways we can arrange letters such that each prefix and suffix have more Bs than As in Python
Suppose we have a string with n number of A's and 2n number of B's. We have to find number of arrangements possible such that in each prefix and each suffix have number of B's greater than or equal to the number of A's
So, if the input is like n = 2, then the output will be 4 because there are two A's and four B's, so possible arrangements are [BBAABB, BABABB, BBABAB, BABBAB].
To solve this, we will follow these steps −
- Define a method solve, this will take n
- if n is same as 1, then
- return 1
- if n is same as 2, then
- return 4
- if n is odd, then
- return find(floor of (n-1)/2)^2
- otherwise,
- return find(floor of n/2)^2
Example
Let us see the following implementation to get better understanding −
def solve(n): if n==1: return 1 if n==2: return 4 if n%2 != 0: return solve((n-1)//2)**2 else: return solve(n//2)**2 n = 2 print(solve(n))
Input
2
Output
4
- Related Articles
- Program to find number of ways we can arrange symbols to get target in Python?
- Program to find number of ways we can merge two lists such that ordering does not change in Python
- Program to find number of ways to arrange n rooks so that they cannot attack each other in Python
- Program to find number of ways we can decode a message in Python
- Program to find number of ways we can split a palindrome in python
- Program to find longest prefix that is also a suffix in C++
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated in Python
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to find number of ways we can select sequence from Ajob Sequence in Python
- Program to count number of words we can generate from matrix of letters in Python
- C++ Program to Find number of Ways to Partition a word such that each word is a Palindrome
- Program to find number of ways we can get n R.s using Indian denominations in Python
- Program to count number of ways we can throw n dices in Python
- Program to find number of ways we can reach to the next floor using stairs in Python
- Program to count number of ways we can distribute coins to workers in Python

Advertisements