计算给定二进制字符串的分数
给定一个二进制字符串str 。对于n-contiguous 1s ,分数更新为score = score + n 2 ,对于n-contiguous 0s ,分数更新为score = score - n 2 。任务是找到完整的二进制字符串的分数。
例子:
Input: str = 11011
Output: 7
score(“11”) – score(“0”) + score(“11”) = 22 – 12 + 22 = 7
Input: str = 1100011
Output: -1
方法:为了解决问题,遍历给定的字符串并计算连续的1s和0s的数量。对于每个 n 1的连续块,将n 2添加到当前分数,类似地,对于每个 n 0的连续块,从当前分数中减去n 2 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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;
}
Java
// 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
Python3
# 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
C#
// 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
PHP
Javascript
输出:
7