Given a binary string str. For n-contiguous 1s the score is updated as score = score + n2 and for n-contiguous 0s, the score is updated as score = score – n2. The task is to find the score of the complete binary string.
Input: str = 11011
score(“11”) – score(“0”) + score(“11”) = 22 – 12 + 22 = 7
Input: str = 1100011
Approach: For solving the problem iterate over the given string and calculate the number of contiguous 1s and 0s. For each contiguous chunk of n 1s add n2 to the current score and similarly for each contiguous chunk of n 0s subtract n2 from the current score.
Below is the implementation of the above approach:
- Calculate sum of all numbers present in a string
- Calculate maximum value using '+' or '*' sign between two numbers in a string
- Python | Check if a given string is binary string or not
- Students with maximum average score of three subjects
- Binary Search a String
- Rotations of a Binary String with Odd Value
- Construct a binary string following the given constraints
- Count of substrings of a binary string containing K ones
- Check if a binary string contains consecutive same or not
- Binary tree to string with brackets
- Check if all the 1's in a binary string are equidistant or not
- Check divisibility of binary string by 2^k
- Convert String into Binary Sequence
- Deletions of "01" or "10" in binary string to make it free from "01" or "10"
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.