Calculate score for the given binary string

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.

Examples:

Input: str = 11011
Output: 7
score(“11”) – score(“0”) + score(“11”) = 22 – 12 + 22 = 7

Input: str = 1100011
Output: -1



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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the score for
// the given binary string
int calcScore(string str)
{
    int score = 0;
    int len = str.length();
  
    // Traverse through string character
    for (int i = 0; i < len;) {
  
        // Initialize current chunk's size
        int chunkSize = 1;
  
        // Get current character
        char currentChar = str[i++];
  
        // Calculate total chunk size
        // of same characters
        while (i < len && str[i] == currentChar) {
            chunkSize++;
            i++;
        }
  
        // Add/subtract pow(chunkSize, 2)
        // depending upon character
        if (currentChar == '1')
            score += pow(chunkSize, 2);
        else
            score -= pow(chunkSize, 2);
    }
  
    // Return the score
    return score;
}
  
// Driver code
int main()
{
    string str = "11011";
    cout << calcScore(str);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG 
{
      
    // Function to return the score for 
    // the given binary string 
    public static int calcScore(String str) 
    
        int score = 0
        int len = str.length(); 
  
        // Traverse through string character 
        for (int i = 0; i < len;) 
        
  
            // Initialize current chunk's size 
            int chunkSize = 1
  
            // Get current character 
            char currentChar = str.charAt(i++); 
  
            // Calculate total chunk size 
            // of same characters 
            while (i < len && str.charAt(i) == currentChar) 
            
                chunkSize++; 
                i++; 
            
  
            // Add/subtract pow(chunkSize, 2) 
            // depending upon character 
            if (currentChar == '1'
                score += Math.pow(chunkSize, 2); 
            else
                score -= Math.pow(chunkSize, 2); 
        }
          
        // Return the score 
        return score; 
    
  
    // Driver code 
    public static void main(String[] args)
    
        String str = "11011"
        System.out.println(calcScore(str)); 
    
}
  
// This code is contributed by Naman_Garg

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of the approach
  
# Function to return the score for
# the given binary string
def calcScore(str):
    score = 0
    len1 = len(str)
  
    # Traverse through string character
    i = 0
    while(i < len1):
          
        # Initialize current chunk's size
        chunkSize = 1
  
        # Get current character
        currentChar = str[i]
        i += 1
  
        # Calculate total chunk size
        # of same characters
        while (i < len1 and str[i] == currentChar):
            chunkSize += 1
            i += 1
  
        # Add/subtract pow(chunkSize, 2)
        # depending upon character
        if (currentChar == '1'):
            score += pow(chunkSize, 2)
        else:
            score -= pow(chunkSize, 2)
  
    # Return the score
    return score
  
# Driver code
if __name__ == '__main__':
    str = "11011"
    print(calcScore(str))
  
# This code is contributed by
# Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
      
class GFG 
{
      
    // Function to return the score for 
    // the given binary string 
    public static int calcScore(String str) 
    
        int score = 0; 
        int len = str.Length; 
  
        // Traverse through string character 
        for (int i = 0; i < len;) 
        
  
            // Initialize current chunk's size 
            int chunkSize = 1; 
  
            // Get current character 
            char currentChar = str[i++]; 
  
            // Calculate total chunk size 
            // of same characters 
            while (i < len && str[i] == currentChar) 
            
                chunkSize++; 
                i++; 
            
  
            // Add/subtract pow(chunkSize, 2) 
            // depending upon character 
            if (currentChar == '1'
                score += (int)Math.Pow(chunkSize, 2); 
            else
                score -= (int)Math.Pow(chunkSize, 2); 
        }
          
        // Return the score 
        return score; 
    
  
    // Driver code 
    public static void Main(String[] args)
    
        String str = "11011"
        Console.WriteLine(calcScore(str)); 
    
}
  
// This code contributed by Rajput-Ji

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Php implementation of the approach 
  
// Function to return the score for 
// the given binary string 
function calcScore($str
    $score = 0; 
    $len = strlen($str); 
  
    // Traverse through string character 
    for ($i = 0; $i < $len😉 
    
  
        // Initialize current chunk's size 
        $chunkSize = 1; 
  
        // Get current character 
        $currentChar = $str[$i++]; 
  
        // Calculate total chunk size 
        // of same characters 
        while ($i < $len && $str[$i] == $currentChar)
        
            $chunkSize++; 
            $i++; 
        
  
        // Add/subtract pow(chunkSize, 2) 
        // depending upon character 
        if ($currentChar == '1'
            $score += pow($chunkSize, 2); 
        else
            $score -= pow($chunkSize, 2); 
    
  
    // Return the score 
    return $score
  
    // Driver code 
    $str = "11011"
    echo calcScore($str); 
      
    // This code is contributed by AnkitRai01
?>

chevron_right


Output:

7


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.