给定长度为N的二进制字符串S和正整数M ,任务是检查是否有可能将字符串循环旋转任意次,以使任何一对连续的1 s最多间隔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对具有超过它们之间的0的M个相邻的1秒,那么它不可能满足给定的条件来解决,因为只有这样的对可以是通过旋转字符串来处理它们之间的所有0都在末尾。
请按照以下步骤解决问题:
- 初始化一个向量,例如V ,以将所有1的索引存储在给定的字符串S中。
- 初始化一个变量,例如count ,以存储一对之间有M个以上0的一对1的数目。
- 遍历给定的字符串S并将所有索引“ 1”存储在向量V中。
- 使用变量i遍历向量V ,从索引1开始,执行以下步骤:
- 将字符串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
输出:
Yes
时间复杂度: O(N)
辅助空间: O(N)