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++
#include <bits/stdc++.h>
using namespace std;
int calcScore(string str)
{
int score = 0;
int len = str.length();
for ( int i = 0; i < len;) {
int chunkSize = 1;
char currentChar = str[i++];
while (i < len && str[i] == currentChar) {
chunkSize++;
i++;
}
if (currentChar == '1' )
score += pow (chunkSize, 2);
else
score -= pow (chunkSize, 2);
}
return score;
}
int main()
{
string str = "11011" ;
cout << calcScore(str);
return 0;
}
|
Java
class GFG
{
public static int calcScore(String str)
{
int score = 0 ;
int len = str.length();
for ( int i = 0 ; i < len;)
{
int chunkSize = 1 ;
char currentChar = str.charAt(i++);
while (i < len && str.charAt(i) == currentChar)
{
chunkSize++;
i++;
}
if (currentChar == '1' )
score += Math.pow(chunkSize, 2 );
else
score -= Math.pow(chunkSize, 2 );
}
return score;
}
public static void main(String[] args)
{
String str = "11011" ;
System.out.println(calcScore(str));
}
}
|
Python3
def calcScore( str ):
score = 0
len1 = len ( str )
i = 0
while (i < len1):
chunkSize = 1
currentChar = str [i]
i + = 1
while (i < len1 and str [i] = = currentChar):
chunkSize + = 1
i + = 1
if (currentChar = = '1' ):
score + = pow (chunkSize, 2 )
else :
score - = pow (chunkSize, 2 )
return score
if __name__ = = '__main__' :
str = "11011"
print (calcScore( str ))
|
C#
using System;
class GFG
{
public static int calcScore(String str)
{
int score = 0;
int len = str.Length;
for ( int i = 0; i < len;)
{
int chunkSize = 1;
char currentChar = str[i++];
while (i < len && str[i] == currentChar)
{
chunkSize++;
i++;
}
if (currentChar == '1' )
score += ( int )Math.Pow(chunkSize, 2);
else
score -= ( int )Math.Pow(chunkSize, 2);
}
return score;
}
public static void Main(String[] args)
{
String str = "11011" ;
Console.WriteLine(calcScore(str));
}
}
|
PHP
<?php
function calcScore( $str )
{
$score = 0;
$len = strlen ( $str );
for ( $i = 0; $i < $len 😉
{
$chunkSize = 1;
$currentChar = $str [ $i ++];
while ( $i < $len && $str [ $i ] == $currentChar )
{
$chunkSize ++;
$i ++;
}
if ( $currentChar == '1' )
$score += pow( $chunkSize , 2);
else
$score -= pow( $chunkSize , 2);
}
return $score ;
}
$str = "11011" ;
echo calcScore( $str );
?>
|
Javascript
<script>
function calcScore(str)
{
var score = 0;
var len = str.length;
for ( var i = 0; i < len;) {
var chunkSize = 1;
var currentChar = str[i++];
while (i < len && str[i] == currentChar) {
chunkSize++;
i++;
}
if (currentChar == '1')
score += Math.pow(chunkSize, 2);
else
score -= Math.pow(chunkSize, 2);
}
return score;
}
var str = "11011" ;
document.write( calcScore(str));
</script>
|
Time complexity: O(N) where N is the length of the given binary string
Auxiliary space: O(1)
Last Updated :
25 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...