 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements
 