给定二进制字符串中唯一索引 10 或 01 子字符串的最大计数
给定一个长度为N的二进制字符串str ,任务是计算当一个字符只能用于一对时,可以从给定的二进制字符串形成的“01”或“10”形式的相邻对的最大数量。
注:相邻对是指由相邻字符组成的对。
例子:
Input: str = “0101110”
Output: 3
Explanation: The three pairs are “01” at the starting first,
“01” starting at 2nd index (0 based indexing)
and “10” at the end of the string.
Notice 2 pairs can also be formed by using “10” from index 1 and “10” at last.
But that does not give the maximum number of adjacent pairs.
Input: str = “0011”
Output: 1
Input: str = “11”
Output: 0
方法:这是一个基于实现的问题。请按照此处提到的步骤解决问题:
- 从左到右遍历字符串。
- 用0初始化对的计数,并将前一个字符视为空闲字符。
- 运行从 1 到字符串大小的循环。
- 检查前一个字符是否与当前字符相反,以及它是否是自由的
- 如果是,则增加对数并将字符设置为非空闲。
- 否则继续循环中的遍历,认为字符是自由的。
- 打印对数。
下面是上述方法的实现。
C++
// C++ code to implement the above approach
#include
using namespace std;
// Count pairs function
void check_pairs(string str)
{
// Initialize pairs with 0
int pairs = 0;
// Previous char is free to pair
bool prev_c = true;
// Traverse string from second position
for (int i = 1; i < str.size(); i++) {
// Check both char are opposite or not
// and also check previous char
// is free or not
if (str[i] != str[i - 1] && prev_c) {
// Once previous char paired
// with other make it false
prev_c = false;
// Increment pairs count
pairs++;
}
else {
// Previous char is free for pair
prev_c = true;
}
}
// Print count of pairs of two characters
cout << pairs;
}
// Driver Code
int main()
{
string str = "0101110";
// Function call
check_pairs(str);
return 0;
}
Java
// Java code to implement the above approach
class GFG
{
// Count pairs function
static void check_pairs(String str)
{
// Initialize pairs with 0
int pairs = 0;
// Previous char is free to pair
boolean prev_c = true;
// Traverse String from second position
for (int i = 1; i < str.length(); i++)
{
// Check both char are opposite or not
// and also check previous char
// is free or not
if (str.charAt(i) != str.charAt(i - 1) && prev_c) {
// Once previous char paired
// with other make it false
prev_c = false;
// Increment pairs count
pairs++;
}
else {
// Previous char is free for pair
prev_c = true;
}
}
// Print count of pairs of two characters
System.out.println(pairs);
}
// Driver Code
public static void main(String args[])
{
String str = "0101110";
// Function call
check_pairs(str);
}
}
// This code is contributed by gfgking
Python3
# python3 code to implement the above approach
# Count pairs function
def check_pairs(str):
# Initialize pairs with 0
pairs = 0
# Previous char is free to pair
prev_c = True
# Traverse string from second position
for i in range(1, len(str)):
# Check both char are opposite or not
# and also check previous char
# is free or not
if (str[i] != str[i - 1] and prev_c):
# Once previous char paired
# with other make it false
prev_c = False
# Increment pairs count
pairs += 1
else:
# Previous char is free for pair
prev_c = True
# Print count of pairs of two characters
print(pairs)
# Driver Code
if __name__ == "__main__":
str = "0101110"
# Function call
check_pairs(str)
# This code is contributed by rakeshsahni
C#
// C# code to implement the above approach
using System;
class GFG
{
// Count pairs function
static void check_pairs(string str)
{
// Initialize pairs with 0
int pairs = 0;
// Previous char is free to pair
bool prev_c = true;
// Traverse string from second position
for (int i = 1; i < str.Length; i++)
{
// Check both char are opposite or not
// and also check previous char
// is free or not
if (str[i] != str[i - 1] && prev_c) {
// Once previous char paired
// with other make it false
prev_c = false;
// Increment pairs count
pairs++;
}
else {
// Previous char is free for pair
prev_c = true;
}
}
// Print count of pairs of two characters
Console.Write(pairs);
}
// Driver Code
public static int Main()
{
string str = "0101110";
// Function call
check_pairs(str);
return 0;
}
}
// This code is contributed by Taranpreet
Javascript
输出
3
时间复杂度: 在)
辅助空间: O(1)