Given a string consisting of only A’s and B’s. We can transform the given string to another string by toggling any character. Thus many transformations of the given string are possible. The task is to find Weight of the maximum weight transformation.
Weight of a sting is calculated using below formula.
Weight of string = Weight of total pairs + weight of single characters - Total number of toggles. Two consecutive characters are considered as pair only if they are different. Weight of a single pair (both character are different) = 4 Weight of a single character = 1
Input: str = "AA" Output: 3 Transformations of given string are "AA", "AB", "BA" and "BB". Maximum weight transformation is "AB" or "BA". And weight is "One Pair - One Toggle" = 4-1 = 3. Input: str = "ABB" Output: 5 Transformations are "ABB", "ABA", "AAB", "AAA", "BBB", "BBA", "BAB" and "BAA" Maximum weight is of original string 4+1 (One Pair + 1 character)
If (n == 1) maxWeight(str[0..n-1]) = 1 Else If str != str // Max of two cases: First character considered separately // First pair considered separately maxWeight(str[0..n-1]) = Max (1 + maxWeight(str[1..n-1]), 4 + getMaxRec(str[2..n-1]) Else // Max of two cases: First character considered separately // First pair considered separately // Since first two characters are same and a toggle is // required to form a pair, 3 is added for pair instead // of 4 maxWeight(str[0..n-1]) = Max (1 + maxWeight(str[1..n-1]), 3 + getMaxRec(str[2..n-1])
If we draw the complete recursion tree, we can observer that many subproblems are solved again and again. Since same suproblems are called again, this problem has Overlapping Subprolems property. So min square sum problem has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems.
Below is a memoization based solution. A lookup table is used to see if a problem is already computed.
Maximum weight of a transformation of AAAAABB is 11
Thanks to Gaurav Ahirwar for providing above solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Smallest length string with repeated replacement of two distinct adjacent
- Count ways to increase LCS length of two strings by one
- Number of subsequences in a string divisible by n
- Number of palindromic paths in a matrix
- Longest repeating and non-overlapping substring
- Maximum profit by buying and selling a share at most k times
- Find minimum possible size of array with given rules for removing elements
- Longest Repeating Subsequence
- Bitmasking and Dynamic Programming | Set 1 (Count ways to assign unique cap to every person)
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Collect maximum points in a grid using two traversals
- Compute sum of digits in all numbers from 1 to n
- How to print maximum number of A's using given four keys
- Minimum Cost Polygon Triangulation
- Overlapping Subproblems Property in Dynamic Programming | DP-1
Improved By : Sam007