给定一个长度为N的二进制字符串S和一个正整数M ,任务是检查是否可以将字符串循环旋转任意次数,使得任何一对连续的1最多被M 0s分隔。如果可能,则打印“是” 。否则,打印“否” 。
例子:
Input: S = “101001”, M = 1
Output: Yes
Explanation: Right shift the characters of the given string by 1 place. Therefore, the string S modifies to “110100”, leaving all pair of consecutive 1s separated by at most M(= 1) 0s.
Input: S = 1001001, M = 1
Output: No
方法:根据观察可以解决给定的问题,如果有超过 1 对相邻的1 之间有超过M个0 ,则不可能满足给定条件,因为只有这样的一对可以通过以这样一种方式旋转字符串来处理,即它们之间的所有0都在末尾。
请按照以下步骤解决问题:
- 初始化一个向量,比如V ,以存储给定字符串S中所有1的索引。
- 初始化一个变量,例如count ,以存储它们之间具有超过M 个0的1对的数量。
- 遍历给定的字符串S并将‘1’ 的所有索引存储在向量V 中。
- 使用变量i从索引1开始遍历向量V ,并执行以下步骤:
- 将字符串S中索引V[i]和V[i – 1]之间的0数存储在变量T 中为(V[i] – V[i – 1] – 1) 。
- 如果T的值大于M ,则将count的值增加1 。
- 如果第一次和最后一次出现‘1’之间的0数大于M ,则将count的值增加1 。
- 完成上述步骤后,如果count的值最多为 1 ,则打印Yes 。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if any pair of
// consecutive 1s can be separated by at
// most M 0s by circular rotation of string S
void rotateString(int n, int m, string s)
{
// Stores the indices of all 1s
vector v;
// Store the number of pairs
// separated by at least M 0s
int cnt = 0;
// Traverse the string
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
// Store the current index
v.push_back(i);
}
}
// Traverse the array containing indices
for (int i = 1; i < (int)v.size(); i++) {
// If the number of 0s > M,
// then increment cnt by 1
if ((v[i] - v[i - 1] - 1) > m) {
// Increment cnt
cnt++;
}
}
// Check if at least M '0's lie between
// the first and last occurrence of '1'
if (v.size() >= 2
&& (n - (v.back() - v[0]) - 1) > m) {
// Increment cnt
cnt++;
}
// If the value of cnt <= 1, then
// rotation of string is possible
if (cnt <= 1) {
cout << "Yes";
}
// Otherwise
else {
cout << "No";
}
}
// Driver Code
int main()
{
string S = "101001";
int M = 1;
int N = S.size();
rotateString(N, M, S);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to check if any pair of
// consecutive 1s can be separated by at
// most M 0s by circular rotation of string S
static void rotateString(int n, int m, String s)
{
// Stores the indices of all 1s
int v[] = new int[n];
// Store the number of pairs
// separated by at least M 0s
int cnt = 0;
int j = 0;
// Traverse the string
for(int i = 0; i < n; i++)
{
if (s.charAt(i) == '1')
{
// Store the current index
v[j] = i;
j += 1;
}
}
// Traverse the array containing indices
for(int i = 1; i < j; i++)
{
// If the number of 0s > M,
// then increment cnt by 1
if ((v[i] - v[i - 1] - 1) > m)
{
// Increment cnt
cnt++;
}
}
// Check if at least M '0's lie between
// the first and last occurrence of '1'
if (j >= 2 && (n - (v[j - 1] - v[0]) - 1) > m)
{
// Increment cnt
cnt++;
}
// If the value of cnt <= 1, then
// rotation of string is possible
if (cnt <= 1)
{
System.out.print("Yes");
}
// Otherwise
else
{
System.out.print("No");
}
}
// Driver Code
public static void main (String[] args)
{
String S = "101001";
int M = 1;
int N = S.length();
rotateString(N, M, S);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
# Function to check if any pair of
# consecutive 1s can be separated by at
# most M 0s by circular rotation of string S
def rotateString(n, m, s):
# Stores the indices of all 1s
v = []
# Store the number of pairs
# separated by at least M 0s
cnt = 0
# Traverse the string
for i in range(n):
if (s[i] == '1'):
# Store the current index
v.append(i)
# Traverse the array containing indices
for i in range(1, len(v)):
# If the number of 0s > M,
# then increment cnt by 1
if ((v[i] - v[i - 1] - 1) > m):
# Increment cnt
cnt += 1
# Check if at least M '0's lie between
# the first and last occurrence of '1'
if (len(v) >= 2 and
(n - (v[-1] - v[0]) - 1) > m):
# Increment cnt
cnt += 1
# If the value of cnt <= 1, then
# rotation of string is possible
if (cnt <= 1):
print("Yes")
# Otherwise
else:
print("No")
# Driver Code
if __name__ == '__main__':
S = "101001"
M = 1
N = len(S)
rotateString(N, M, S)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check if any pair of
// consecutive 1s can be separated by at
// most M 0s by circular rotation of string S
static void rotateString(int n, int m, string s)
{
// Stores the indices of all 1s
List v = new List();
// Store the number of pairs
// separated by at least M 0s
int cnt = 0;
// Traverse the string
for(int i = 0; i < n; i++)
{
if (s[i] == '1')
{
// Store the current index
v.Add(i);
}
}
// Traverse the array containing indices
for(int i = 1; i < v.Count; i++)
{
// If the number of 0s > M,
// then increment cnt by 1
if ((v[i] - v[i - 1] - 1) > m)
{
// Increment cnt
cnt++;
}
}
// Check if at least M '0's lie between
// the first and last occurrence of '1'
if (v.Count >= 2 &&
(n - (v[v.Count - 1] - v[0]) - 1) > m)
{
// Increment cnt
cnt++;
}
// If the value of cnt <= 1, then
// rotation of string is possible
if (cnt <= 1)
{
Console.Write("Yes");
}
// Otherwise
else
{
Console.Write("No");
}
}
// Driver Code
public static void Main()
{
string S = "101001";
int M = 1;
int N = S.Length;
rotateString(N, M, S);
}
}
// This code is contributed by ipg2016107
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live