在 K 中的二进制字符串中可以到达的最远位置通过在交替数字上跳转
给定一个长度为N的二进制字符串S和一个整数K ,任务是计算从恰好 K次跳转的第一个位置开始可以到达的最远位置。
只有在以下情况下才能从索引 i 跳转到 j:
- 我!= j
- 如果其中一个的字符是“0”而另一个是“1”。
例子:
Input: S = “100101”, K = 2
Output: 6
Explanation: Following steps can be taken: 1 -> 5 -> 6, Therefore 6th index can be reached in exactly 2 jumps
Input: S = “10111”, K = 1
Output: 2
Explanation: Following steps can be taken: 1 -> 2, Therefore 2nd index can be reached in exactly 2 jumps
处理方法:问题的主要观察是0和1的连续模式可以用单0或单1代替,因为不能在相似字符之间跳转。在替换了这些连续的0s和1s模式之后,现在可以简单地检查新模式是否说str形成的小于等于K则不可能通过K移动到达某个点,否则,位置只是第一个在包含字符str[K]的实际模式中从右侧开始的位置。
请按照以下步骤解决问题:
- 从给定字符串i = 0 到 i = N-1 的开始到结束迭代,并在用单个 0 替换所有连续子模式 0 和用单个 1 替换 1 后生成新字符串str。
- 现在,检查新形成的字符串的长度是否小于等于 K 然后打印-1
- 否则,如果新形成的字符串的长度大于K,则只需从右侧打印字符str[K]从 1 开始的位置。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the farthest point
void get(string s, int k)
{
// Store the answer
int ans = 0;
// Find the new string str
string str = "";
str += s[0];
// Variables to count the zeroes and ones
int cnt1 = 0, cnt0 = 0;
int n = s.length();
// Boolean variable to keep track of the subpattern
bool isOne;
if (s[0] == '0')
isOne = false;
// Iterate over the string and remove the
// continuous patterns of 0s and 1s
for (int i = 1; i < n; i++) {
if (s[i] == '0' && isOne) {
str += s[i];
isOne = !isOne;
}
else if (s[i] == '1' && !isOne) {
str += s[i];
isOne = !isOne;
}
}
// Count the number of zeroes and ones
for (int i = 0; i < n; i++) {
if (str[i] == '0')
cnt0++;
else
cnt1++;
}
// Check if the K jumps are not possible
if (str.length() <= k) {
cout << -1 << endl;
return;
}
// If K jumps are possible
for (int i = n - 1; i >= 0; i--) {
if (s[i] == str[k]) {
ans = i + 1;
break;
}
}
cout << ans + 1 << endl;
}
// Driver Code
int main()
{
string s = "100101";
int k = 2;
get(s, k);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find the farthest point
static void get(String s, int k)
{
// Store the answer
int ans = 0;
// Find the new string str
String str = "";
str += s.charAt(0);
// Variables to count the zeroes and ones
int cnt1 = 0, cnt0 = 0;
int n = s.length();
// Boolean variable to keep track of the subpattern
boolean isOne = false;
if (s.charAt(0) == '0')
isOne = false;
// Iterate over the string and remove the
// continuous patterns of 0s and 1s
for (int i = 1; i < n; i++) {
if (s.charAt(i) == '0' && isOne) {
str += s.charAt(i);
isOne = !isOne;
}
else if (s.charAt(i) == '1' && !isOne) {
str += s.charAt(i);
isOne = !isOne;
}
}
// Count the number of zeroes and ones
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '0')
cnt0++;
else
cnt1++;
}
// Check if the K jumps are not possible
if (str.length() <= k) {
System.out.print(-1);
return;
}
// If K jumps are possible
for (int i = n - 1; i >= 0; i--) {
if (s.charAt(i) == str.charAt(k)) {
ans = i + 1;
break;
}
}
System.out.print(ans + 1);
}
// Driver Code
public static void main(String args[])
{
String s = "100101";
int k = 2;
get(s, k);
}
}
// This code is contributed by Samim Hossain Mondal
Python3
# Python3 program for the above approach
# Function to find the farthest point
def get(s, k) :
# Store the answer
ans = 0;
# Find the new string str
string = "";
string += s[0];
# Variables to count the zeroes and ones
cnt1 = 0; cnt0 = 0;
n = len(s);
# Boolean variable to keep track of the subpattern
isOne=False;
if (s[0] == '0') :
isOne = False;
# Iterate over the string and remove the
# continuous patterns of 0s and 1s
for i in range(1, n) :
if (s[i] == '0' and isOne) :
string += s[i];
isOne = not isOne;
elif (s[i] == '1' and (not isOne)) :
string += s[i];
isOne = not isOne;
# Count the number of zeroes and ones
for i in range(len(string)) :
if (string[i] == '0') :
cnt0 += 1;
else :
cnt1 += 1;
# Check if the K jumps are not possible
if (len(string) <= k) :
print(-1) ;
return;
# If K jumps are possible
for i in range(n - 1, -1, -1) :
if (s[i] == string[k]) :
ans = i + 1;
break;
print(ans + 1);
# Driver Code
if __name__ == "__main__" :
s = "100101";
k = 2;
get(s, k);
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the farthest point
static void get(string s, int k)
{
// Store the answer
int ans = 0;
// Find the new string str
string str = "";
str += s[0];
// Variables to count the zeroes and ones
int cnt1 = 0, cnt0 = 0;
int n = s.Length;
// Bool variable to keep track of the subpattern
bool isOne = false;
if (s[0] == '0')
isOne = false;
// Iterate over the string and remove the
// continuous patterns of 0s and 1s
for (int i = 1; i < n; i++) {
if (s[i] == '0' && isOne) {
str += s[i];
isOne = !isOne;
}
else if (s[i] == '1' && !isOne) {
str += s[i];
isOne = !isOne;
}
}
// Count the number of zeroes and ones
for (int i = 0; i < str.Length; i++) {
if (str[i] == '0')
cnt0++;
else
cnt1++;
}
// Check if the K jumps are not possible
if (str.Length <= k) {
Console.Write(-1);
return;
}
// If K jumps are possible
for (int i = n - 1; i >= 0; i--) {
if (s[i] == str[k]) {
ans = i + 1;
break;
}
}
Console.Write(ans + 1);
}
// Driver Code
public static void Main()
{
string s = "100101";
int k = 2;
get(s, k);
}
}
// This code is contributed by Samim Hossain Mondal
Javascript
输出
6
时间复杂度: O(N)
辅助空间: O(1)