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
- Longest sub string of 0's in a binary string which is repeated K times
- Python | Check if a given string is binary string or not
- Largest sub-string of a binary string divisible by 2
- 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
- Check if a binary string contains consecutive same or not
- Check divisibility of binary string by 2^k
- Convert String into Binary Sequence
- Count of 1-bit and 2-bit characters in the given binary string
- Count of substrings of a binary string containing K ones
- Binary tree to string with brackets
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.