<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/maximum-weight-transformation-of-string-6hjmi8pgb" -->

---
title: Maximum Weight Transformation of String | daily.dev
description: This tutorial explains a dynamic programming solution for maximizing the 'weight' of a binary string made of 'A' and 'B' characters. An alternating pair like...
canonical: https://daily.dev/posts/maximum-weight-transformation-of-string-6hjmi8pgb
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Maximum Weight Transformation of String | daily.dev
og:description: This tutorial explains a dynamic programming solution for maximizing the 'weight' of a binary string made of 'A' and 'B' characters. An alternating pair like...
og:url: https://daily.dev/posts/maximum-weight-transformation-of-string-6hjmi8pgb
og:image: https://api.daily.dev/og/posts/6HjMi8PGb.png
og:image:alt: Maximum Weight Transformation of String
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Maximum Weight Transformation of String

**[C\# Corner](https://daily.dev/sources/csharpcorner)** · 4 min read · 0 upvotes · 0 comments

## Summary

This tutorial explains a dynamic programming solution for maximizing the 'weight' of a binary string made of 'A' and 'B' characters. An alternating pair like 'AB' or 'BA' contributes 4 to the weight, an unpaired character contributes 1, and each toggle (flipping A to B or vice versa) costs 1. For 'AA', toggling one character to form 'AB' gives a final weight of 3. The DP recurrence is dp[i] = max(dp[i-1] + 1, dp[i-2] + pairWeight), where pairWeight is 4 for already-alternating pairs and 3 for matching pairs after a toggle. Full Java code implementing this is included.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.c-sharpcorner.com/article/maximum-weight-transformation-of-string>

## Questions this post answers

### How do I calculate the maximum weight of a string made of A and B characters where alternating pairs score higher?

Use dynamic programming where dp[i] tracks the max weight using the first i characters. At each position, either leave the current character unpaired (dp[i-1] + 1) or pair the last two characters (dp[i-2] + pairWeight), where pairWeight is 4 for already-alternating pairs like AB or BA, and 3 for matching pairs like AA or BB after a one-character toggle.

_daily.dev surfaces more dynamic programming walkthroughs for developers prepping algorithm interview questions._

### Why does toggling one character in a matching pair like AA still result in a weight of 3 instead of 4?

Turning AA into AB forms a valid alternating pair worth 4, but the toggle itself costs 1 point, giving a net effective contribution of 3. Every toggle decreases the final weight by 1, so same-character pairs are always worth one less than already-alternating pairs.

_Developers working through weighted string DP problems can find similar breakdowns on daily.dev._

## Similar posts on daily.dev

- [Count Substrings with More 1's than 0's](https://daily.dev/posts/count-substrings-with-more-1-s-than-0-s-k63z1kpmf) · C\# Corner · 0 upvotes · 0 comments
- [Maximum Product Cutting of a Rope Using a Greedy Mathematical Approach](https://daily.dev/posts/maximum-product-cutting-of-a-rope-using-a-greedy-mathematical-approach-ydjjbjcwb) · C\# Corner · 0 upvotes · 0 comments
- [Solving the 3Blue1Brown String Probability Problem \(Without AI\)](https://daily.dev/posts/solving-the-3blue1brown-string-probability-problem-without-ai--thczbuec0) · Towards Data Science · 0 upvotes · 0 comments
- [Palindrome with Replacement – Minimum Adjacent Difference](https://daily.dev/posts/palindrome-with-replacement-minimum-adjacent-difference-rmvpsbgdj) · C\# Corner · 0 upvotes · 0 comments

---

Tags: [#java](https://daily.dev/tags/java), [#algorithms](https://daily.dev/tags/algorithms), [#dynamic-programming](https://daily.dev/tags/dynamic-programming)

[View this post on daily.dev](https://daily.dev/posts/maximum-weight-transformation-of-string-6hjmi8pgb)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Maximum Weight Transformation of String","url":"https://daily.dev/posts/maximum-weight-transformation-of-string-6hjmi8pgb","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/maximum-weight-transformation-of-string-6hjmi8pgb"},"datePublished":"2026-10-27T05:10:29.682Z","dateModified":"2026-10-27T05:10:59.333Z","description":"This tutorial explains a dynamic programming solution for maximizing the 'weight' of a binary string made of 'A' and 'B' characters. An alternating pair like...","image":"https://media.daily.dev/image/upload/s--ZrL_HSsR--/f_auto/v1722860399/public/Placeholder%2006","thumbnailUrl":"https://media.daily.dev/image/upload/s--ZrL_HSsR--/f_auto/v1722860399/public/Placeholder%2006","isAccessibleForFree":true,"articleSection":"C# Corner","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"C# Corner","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/ada2d584df6241748fc4e71878dc70a3","url":"https://daily.dev/sources/csharpcorner"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/maximum-weight-transformation-of-string-6hjmi8pgb","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"java,algorithms,dynamic-programming","timeRequired":"PT4M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"C# Corner","item":"https://daily.dev/sources/csharpcorner"},{"@type":"ListItem","position":3,"name":"Maximum Weight Transformation of String"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/maximum-weight-transformation-of-string-6hjmi8pgb#faq","mainEntity":[{"@type":"Question","name":"How do I calculate the maximum weight of a string made of A and B characters where alternating pairs score higher?","acceptedAnswer":{"@type":"Answer","text":"Use dynamic programming where dp[i] tracks the max weight using the first i characters. At each position, either leave the current character unpaired (dp[i-1] + 1) or pair the last two characters (dp[i-2] + pairWeight), where pairWeight is 4 for already-alternating pairs like AB or BA, and 3 for matching pairs like AA or BB after a one-character toggle. daily.dev surfaces more dynamic programming walkthroughs for developers prepping algorithm interview questions."}},{"@type":"Question","name":"Why does toggling one character in a matching pair like AA still result in a weight of 3 instead of 4?","acceptedAnswer":{"@type":"Answer","text":"Turning AA into AB forms a valid alternating pair worth 4, but the toggle itself costs 1 point, giving a net effective contribution of 3. Every toggle decreases the final weight by 1, so same-character pairs are always worth one less than already-alternating pairs. Developers working through weighted string DP problems can find similar breakdowns on daily.dev."}}]}
```

