📌  相关文章
📜  给定二进制字符串中相邻 1 之间的最大距离

📅  最后修改于: 2022-05-13 01:56:09.445000             🧑  作者: Mango

给定二进制字符串中相邻 1 之间的最大距离

给定一个包含 N 个字符的二进制字符串S ,任务是找到两个相邻1之间的最大距离。

例子:

方法:给定的问题是一个基于实现的问题。这个想法是将1 的所有索引以升序存储在一个向量中。因此可以观察到,所需的答案将是索引向量中连续整数之差最大值

下面是上述方法的实现:

C++14
// C++ program of the above approach
#include 
using namespace std;
 
// Function to find the maximum
// distance between two adjacent
// 1's in a given binary string
int maxDist(string S)
{
    // Stores the required answer
    int maxLen = INT_MIN;
 
    // Vector to store indices
    vector indices;
 
    // Loop to traverse string
    for (int i = 0; i < S.length(); i++) {
        if (S[i] == '1')
            indices.push_back(i);
    }
 
    // Loop to reverse the
    // index vector
    for (int i = 1; i < indices.size(); i++)
 
        // Update maximum distance
        maxLen = max(maxLen, indices[i] -
                     indices[i - 1]);
 
    // Return Answer
    return maxLen == INT_MIN ? -1 : maxLen;
}
 
// Driver Code
int main()
{
    string S = "1010010";
    cout << maxDist(S);
    return 0;
}


Java
// Java program of the above approach
import java.io.*;
import java.util.*;
 
class GFG
{
 
  // Function to find the maximum
  // distance between two adjacent
  // 1's in a given binary String
  public static int maxDist(String S)
  {
     
    // Stores the required answer
    int maxLen = Integer.MIN_VALUE;
 
    // Vector to store indices
    Vector indices = new Vector();
 
    // Loop to traverse String
    for (int i = 0; i < S.length(); i++) {
      if (S.charAt(i) == '1')
        indices.add(i);
    }
 
    // Loop to reaverse the
    // index vector
    for (int i = 1; i < indices.size(); i++)
 
      // Update maximum distance
      maxLen = Math.max(maxLen, indices.get(i) -
                        indices.get(i - 1));
 
    // Return Answer
    return maxLen == Integer.MIN_VALUE ? -1 : maxLen;
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    String S = "1010010";
    System.out.println(maxDist(S));
  }
}
 
// This code is contributed by subhamsingh10.


Python3
# Python code for the above approach
 
# Function to find the maximum
# distance between two adjacent
# 1's in a given binary string
def maxDist(S):
 
    # Stores the required answer
    maxLen = 10 ** -9
 
    # Vector to store indices
    indices = []
 
    # Loop to traverse string
    for i in range(len(S)):
        if S[i] == "1":
            indices.append(i)
 
    # Loop to reaverse the
    # index vector
    for i in range(1, len(indices)):
 
        # Update maximum distance
        maxLen = max(maxLen, indices[i] - indices[i - 1])
 
    # Return Answer
    return -1 if (maxLen == 10 ** -9) else maxLen
 
# Driver Code
S = "1010010"
print(maxDist(S))
 
# This code is contributed by gfgking


C#
// C# program for above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
  // Function to find the maximum
  // distance between two adjacent
  // 1's in a given binary string
  static int maxDist(string S)
  {
 
    // Stores the required answer
    int maxLen = Int32.MinValue;
 
    // Vector to store indices
    List indices = new List();
 
    // Loop to traverse string
    for (int i = 0; i < S.Length; i++) {
      if (S[i] == '1')
        indices.Add(i);
    }
 
    // Loop to reaverse the
    // index vector
    for (int i = 1; i < indices.Count; i++)
 
      // Update maximum distance
      maxLen = Math.Max(maxLen, indices[i] -
                        indices[i - 1]);
 
    // Return Answer
    if(maxLen == Int32.MinValue)
      return  -1;
    else
      return maxLen;
  }
 
  // Driver code
  static public void Main ()
  {
 
    string S = "1010010";
    Console.WriteLine(maxDist(S));
  }
}
// This code is contributed by hrithikgarg03188


Javascript


C++14
// C++ program of the above approach
#include 
using namespace std;
 
// Function to find the maximum
// distance between two adjacent
// 1's in a given binary string
int maxDist(string S)
{
    // Stores the required answer
    int maxLen=0,i,start=0;
 
    // Loop to find first occurrence of '1' string
    for ( i = 0; i < S.length(); i++) {
        if (S[i] == '1')
        {
            start=i;
            break;
        }
    }
 
    // Loop to traverse remaining
    // indices of character '1'
    for (; i < S.length(); i++){
     
        // Update maximum distance
            if (S[i] == '1')
        {
            maxLen=max(maxLen,i-start);
            start=i;
        }
    }       
 
    // Return Answer
    return maxLen == 0 ? -1 : maxLen;
}
 
// Driver Code
int main()
{
    string S = "100000";
    cout << maxDist(S);
    return 0;
}


Java
// Java program of the above approach
 
import java.util.*;
 
class GFG{
 
// Function to find the maximum
// distance between two adjacent
// 1's in a given binary String
static int maxDist(String S)
{
    // Stores the required answer
    int maxLen=0,i,start=0;
 
    // Loop to find first occurrence of '1' String
    for ( i = 0; i < S.length(); i++) {
        if (S.charAt(i) == '1')
        {
            start=i;
            break;
        }
    }
 
    // Loop to traverse remaining
    // indices of character '1'
    for (; i < S.length(); i++){
     
        // Update maximum distance
            if (S.charAt(i) == '1')
        {
            maxLen=Math.max(maxLen,i-start);
            start=i;
        }
    }       
 
    // Return Answer
    return maxLen == 0 ? -1 : maxLen;
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "100000";
    System.out.print(maxDist(S));
}
}
 
// This code contributed by Rajput-Ji


C#
// C# program of the above approach
using System;
public class GFG {
 
  // Function to find the maximum
  // distance between two adjacent
  // 1's in a given binary String
  static public int maxDist(String S)
  {
 
    // Stores the required answer
    int maxLen = 0, i, start = 0;
 
    // Loop to find first occurrence of '1' String
    for (i = 0; i < S.Length; i++) {
      if (S[i] == '1') {
        start = i;
        break;
      }
    }
 
    // Loop to traverse remaining
    // indices of character '1'
    for (; i < S.Length; i++) {
 
      // Update maximum distance
      if (S[i] == '1') {
        maxLen = Math.Max(maxLen, i - start);
        start = i;
      }
    }
 
    // Return Answer
    return maxLen == 0 ? -1 : maxLen;
  }
 
  // Driver Code
  public static void Main(String[] args) {
    String S = "100000";
    Console.Write(maxDist(S));
  }
}
 
// This code is contributed by Rajput-Ji


Javascript


Python3
# Python program of the above approach
 
 
 
 
 
# Function to find the maximum
# distance between two adjacent
# 1's in a given binary String
def maxDist(S):
    # Stores the required answer
    maxLen = 0;
    start = 0;
 
    # Loop to find first occurrence of '1' String
    for i in range(len(S)):
        if (S[i] == '1'):
            start = i;
            break;
         
     
 
    # Loop to traverse remaining
    # indices of character '1'
    for i in range(start,len(S)):
 
        # Update maximum distance
        if (S[i] == '1'):
            maxLen = max(maxLen, i - start);
            start = i;
         
     
 
    # Return Answer
    if(maxLen == 0):
        return -1;
    else:
        return maxLen;
 
 
# Driver Code
if __name__ == '__main__':
    S = "100000";
    print(maxDist(S));
 
 
# This code contributed by Rajput-Ji


输出
3

时间复杂度: O(N)
辅助空间: O(N),其中 N 是二进制字符串的长度。

空间高效方法:上述方法也可以在不使用任何额外空间(向量)的情况下实现。唯一的变化是每次我们在二进制字符串中找到1时,通过严格的差异存储和更新来实现最大距离。

C++14

// C++ program of the above approach
#include 
using namespace std;
 
// Function to find the maximum
// distance between two adjacent
// 1's in a given binary string
int maxDist(string S)
{
    // Stores the required answer
    int maxLen=0,i,start=0;
 
    // Loop to find first occurrence of '1' string
    for ( i = 0; i < S.length(); i++) {
        if (S[i] == '1')
        {
            start=i;
            break;
        }
    }
 
    // Loop to traverse remaining
    // indices of character '1'
    for (; i < S.length(); i++){
     
        // Update maximum distance
            if (S[i] == '1')
        {
            maxLen=max(maxLen,i-start);
            start=i;
        }
    }       
 
    // Return Answer
    return maxLen == 0 ? -1 : maxLen;
}
 
// Driver Code
int main()
{
    string S = "100000";
    cout << maxDist(S);
    return 0;
}

Java

// Java program of the above approach
 
import java.util.*;
 
class GFG{
 
// Function to find the maximum
// distance between two adjacent
// 1's in a given binary String
static int maxDist(String S)
{
    // Stores the required answer
    int maxLen=0,i,start=0;
 
    // Loop to find first occurrence of '1' String
    for ( i = 0; i < S.length(); i++) {
        if (S.charAt(i) == '1')
        {
            start=i;
            break;
        }
    }
 
    // Loop to traverse remaining
    // indices of character '1'
    for (; i < S.length(); i++){
     
        // Update maximum distance
            if (S.charAt(i) == '1')
        {
            maxLen=Math.max(maxLen,i-start);
            start=i;
        }
    }       
 
    // Return Answer
    return maxLen == 0 ? -1 : maxLen;
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "100000";
    System.out.print(maxDist(S));
}
}
 
// This code contributed by Rajput-Ji

C#

// C# program of the above approach
using System;
public class GFG {
 
  // Function to find the maximum
  // distance between two adjacent
  // 1's in a given binary String
  static public int maxDist(String S)
  {
 
    // Stores the required answer
    int maxLen = 0, i, start = 0;
 
    // Loop to find first occurrence of '1' String
    for (i = 0; i < S.Length; i++) {
      if (S[i] == '1') {
        start = i;
        break;
      }
    }
 
    // Loop to traverse remaining
    // indices of character '1'
    for (; i < S.Length; i++) {
 
      // Update maximum distance
      if (S[i] == '1') {
        maxLen = Math.Max(maxLen, i - start);
        start = i;
      }
    }
 
    // Return Answer
    return maxLen == 0 ? -1 : maxLen;
  }
 
  // Driver Code
  public static void Main(String[] args) {
    String S = "100000";
    Console.Write(maxDist(S));
  }
}
 
// This code is contributed by Rajput-Ji

Javascript


Python3

# Python program of the above approach
 
 
 
 
 
# Function to find the maximum
# distance between two adjacent
# 1's in a given binary String
def maxDist(S):
    # Stores the required answer
    maxLen = 0;
    start = 0;
 
    # Loop to find first occurrence of '1' String
    for i in range(len(S)):
        if (S[i] == '1'):
            start = i;
            break;
         
     
 
    # Loop to traverse remaining
    # indices of character '1'
    for i in range(start,len(S)):
 
        # Update maximum distance
        if (S[i] == '1'):
            maxLen = max(maxLen, i - start);
            start = i;
         
     
 
    # Return Answer
    if(maxLen == 0):
        return -1;
    else:
        return maxLen;
 
 
# Driver Code
if __name__ == '__main__':
    S = "100000";
    print(maxDist(S));
 
 
# This code contributed by Rajput-Ji
输出
-1

时间复杂度: O(N)

辅助空间: O(1)其中 N 是二进制字符串的长度。