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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Students with maximum average score of three subjects
- Score of Parentheses using Tree
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Calculate the frequency of each word in the given string
- Calculate Sum of ratio of special characters to length of substrings of the given string
- Check if binary representations of 0 to N are present as substrings in given binary string
- 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
- Calculate weight of parenthesis based on the given conditions
- Minimum given operations required to convert a given binary string to all 1's
- Check if given Binary string follows then given condition or not
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Restore original String from given Encrypted String by the given operations
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Longest sub string of 0's in a binary string which is repeated K times
- Largest sub-string of a binary string divisible by 2
- Print string after removing all (“10” or “01”) from the binary string
- String obtained by reversing and complementing a Binary string K times
- Calculate the difficulty of a sentence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.