Maximum weight transformation of a given string
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 string 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
Examples :
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)
maxWeight(str[0..n-1]) = 1
Else If str[0] != str[1]
// 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 subproblems are called again, this problem has Overlapping Subproblems 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.
C++
#include<bits/stdc++.h>
using namespace std;
int getMaxRec(string &str, int i, int n,
int lookup[])
{
if (i >= n) return 0;
if (lookup[i] != -1) return lookup[i];
int ans = 1 + getMaxRec(str, i + 1, n,
lookup);
if (i + 1 < n)
{
if (str[i] != str[i+1])
ans = max(4 + getMaxRec(str, i + 2,
n, lookup), ans);
else ans = max(3 + getMaxRec(str, i + 2,
n, lookup), ans);
}
return lookup[i] = ans;
}
int getMaxWeight(string str)
{
int n = str.length();
int lookup[n];
memset (lookup, -1, sizeof lookup);
return getMaxRec(str, 0, str.length(),
lookup);
}
int main()
{
string str = "AAAAABB" ;
cout << "Maximum weight of a transformation of "
<< str << " is " << getMaxWeight(str);
return 0;
}
|
Java
class GFG {
static int getMaxRec(String str, int i,
int n, int [] lookup)
{
if (i >= n)
{
return 0 ;
}
if (lookup[i] != - 1 )
{
return lookup[i];
}
int ans = 1 + getMaxRec(str, i + 1 ,
n, lookup);
if (i + 1 < n)
{
if (str.charAt(i) != str.charAt(i + 1 ))
{
ans = Math.max( 4 + getMaxRec(str, i + 2 ,
n, lookup), ans);
}
else
{
ans = Math.max( 3 + getMaxRec(str, i + 2 ,
n, lookup), ans);
}
}
return lookup[i] = ans;
}
static int getMaxWeight(String str)
{
int n = str.length();
int [] lookup = new int [n];
for ( int i = 0 ; i < n; i++)
{
lookup[i] = - 1 ;
}
return getMaxRec(str, 0 , str.length(),
lookup);
}
public static void main(String[] args)
{
String str = "AAAAABB" ;
System.out.println( "Maximum weight of a"
+ " transformation of "
+ str + " is "
+ getMaxWeight(str));
}
}
|
Python3
def getMaxRec(string, i, n, lookup):
if i > = n:
return 0
if lookup[i] ! = - 1 :
return lookup[i]
ans = 1 + getMaxRec(string, i + 1 , n,
lookup)
if i + 1 < n:
if string[i] ! = string[i + 1 ]:
ans = max ( 4 + getMaxRec(string, i + 2 ,
n, lookup), ans)
else :
ans = max ( 3 + getMaxRec(string, i + 2 ,
n, lookup), ans)
lookup[i] = ans
return ans
def getMaxWeight(string):
n = len (string)
lookup = [ - 1 ] * (n)
return getMaxRec(string, 0 ,
len (string), lookup)
if __name__ = = "__main__" :
string = "AAAAABB"
print ( "Maximum weight of a transformation of" ,
string, "is" , getMaxWeight(string))
|
C#
using System;
class GFG
{
static int getMaxRec( string str, int i,
int n, int []lookup)
{
if (i >= n) return 0;
if (lookup[i] != -1) return lookup[i];
int ans = 1 + getMaxRec(str, i + 1,
n, lookup);
if (i + 1 < n)
{
if (str[i] != str[i + 1])
ans = Math.Max(4 + getMaxRec(str, i + 2,
n, lookup), ans);
else ans = Math.Max(3 + getMaxRec(str, i + 2,
n, lookup), ans);
}
return lookup[i] = ans;
}
static int getMaxWeight( string str)
{
int n = str.Length;
int [] lookup = new int [n];
for ( int i = 0 ; i < n ; i++)
lookup[i] = -1;
return getMaxRec(str, 0, str.Length,
lookup);
}
public static void Main()
{
string str = "AAAAABB" ;
Console.Write( "Maximum weight of a" +
" transformation of " +
str + " is " +
getMaxWeight(str));
}
}
|
Javascript
<script>
function getMaxRec(str, i, n, lookup)
{
if (i >= n)
{
return 0;
}
if (lookup[i] != -1)
{
return lookup[i];
}
let ans = 1 + getMaxRec(str, i + 1, n, lookup);
if (i + 1 < n)
{
if (str[i] != str[i + 1])
{
ans = Math.max(4 + getMaxRec(str, i + 2,
n, lookup), ans);
}
else
{
ans = Math.max(3 + getMaxRec(str, i + 2,
n, lookup), ans);
}
}
return lookup[i] = ans;
}
function getMaxWeight(str)
{
let n = str.length;
let lookup = new Array(n);
lookup.fill(0);
for (let i = 0; i < n; i++)
{
lookup[i] = -1;
}
return getMaxRec(str, 0, str.length, lookup);
}
let str = "AAAAABB" ;
document.write( "Maximum weight of a"
+ " transformation of "
+ str + " is "
+ getMaxWeight(str));
</script>
|
Output:
Maximum weight of a transformation of AAAAABB is 11
Thanks to Gaurav Ahirwar for providing above solution.
Last Updated :
04 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...