给定二进制字符串str ,任务是检查字符串中的所有1是否等距。术语等距是指每两个相邻的1之间的距离是相同的。请注意,该字符串至少包含两个1 。
例子:
Input: str = “00111000”
Output: Yes
The distance between all the 1’s is same and is equal to 1.
Input: str = “0101001”
Output: No
The distance between the 1st and the 2nd 1’s is 2
and the distance between the 2nd and the 3rd 1’s is 3.
做法:店内所有1点的向量中字符串中的位置,然后检查是否每两个连续位置之间的差异是相同与否。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
#include
using namespace std;
// Function that returns true if all the 1's
// in the binary string s are equidistant
bool check(string s, int l)
{
// Initialize vector to store
// the position of 1's
vector pos;
for (int i = 0; i < l; i++) {
// Store the positions of 1's
if (s[i] == '1')
pos.push_back(i);
}
// Size of the position vector
int t = pos.size();
for (int i = 1; i < t; i++) {
// If condition isn't satisfied
if ((pos[i] - pos[i - 1]) != (pos[1] - pos[0]))
return false;
}
return true;
}
// Drivers code
int main()
{
string s = "100010001000";
int l = s.length();
if (check(s, l))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function that returns true if all the 1's
// in the binary string s are equidistant
static boolean check(String s, int l)
{
// Initialize vector to store
// the position of 1's
Vector pos = new Vector();
for (int i = 0; i < l; i++)
{
// Store the positions of 1's
if (s.charAt(i)== '1')
pos.add(i);
}
// Size of the position vector
int t = pos.size();
for (int i = 1; i < t; i++)
{
// If condition isn't satisfied
if ((pos.get(i) - pos.get(i-1)) != (pos.get(1) - pos.get(0)))
return false;
}
return true;
}
// Drivers code
public static void main(String args[])
{
String s = "100010001000";
int l = s.length();
if (check(s, l))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation of the approach
# Function that returns true if all the 1's
# in the binary s are equidistant
def check(s, l):
# Initialize vector to store
# the position of 1's
pos = []
for i in range(l):
# Store the positions of 1's
if (s[i] == '1'):
pos.append(i)
# Size of the position vector
t = len(pos)
for i in range(1, t):
# If condition isn't satisfied
if ((pos[i] -
pos[i - 1]) != (pos[1] -
pos[0])):
return False
return True
# Driver code
s = "100010001000"
l = len(s)
if (check(s, l)):
print("Yes")
else:
print("No")
# This code is contributed
# by mohit kumar
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function that returns true if all the 1's
// in the binary string s are equidistant
static bool check(String s, int l)
{
// Initialize vector to store
// the position of 1's
List pos = new List();
for (int i = 0; i < l; i++)
{
// Store the positions of 1's
if (s[i]== '1')
{
pos.Add(i);
}
}
// Size of the position vector
int t = pos.Count;
for (int i = 1; i < t; i++)
{
// If condition isn't satisfied
if ((pos[i] - pos[i - 1]) != (pos[1] - pos[0]))
return false;
}
return true;
}
// Drivers code
public static void Main(String []args)
{
String s = "100010001000";
int l = s.Length;
if (check(s, l))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
/* This code contributed by PrinciRaj1992 */
Javascript
输出:
Yes
时间复杂度: O(N),其中N是字符串的长度。
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。