📜  包含“1”的最长子串

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

包含“1”的最长子串

给定一个二进制字符串,任务是打印仅包含'1'的最长子字符串的长度。

例子:

方法:遍历字符串并计算遇到的连续1的数量。将连续1的最大数量存储在变量中,例如max 。最后,打印得到的max的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find length of
// longest substring containing '1'
int maxlength(string s)
{
    int n = s.length(), i, j;
    int ans = 0;
    for (i = 0; i <= n - 1; i++) {
 
        // Count the number of contiguous 1's
        if (s[i] == '1') {
 
            int count = 1;
            for (j = i + 1; j <= n - 1
                            && s[j] == '1';
                 j++)
                count++;
            ans = max(ans, count);
        }
    }
    return ans;
}
 
// Driver Code
int main()
{
    string s = "11101110";
 
    cout << maxlength(s) << endl;
    return 0;
}


Java
// Java program to find length
// of longest substring containing '1'
 
class GFG {
 
    // Function to find length of
    // of longest substring containing '1'
    static int maxlength(String s)
    {
        int n = s.length(), i, j;
        int ans = 0;
        for (i = 0; i <= n - 1; i++) {
 
            // Count the number of contiguous 1's
            if (s.charAt(i) == '1') {
 
                int count = 1;
                for (j = i + 1;
                     j <= n - 1 && s.charAt(j) == '1'; j++)
                    count++;
                ans = Math.max(ans, count);
            }
        }
        return ans;
    }
 
    public static void main(String[] args)
    {
        String s = "11101110";
 
        System.out.println(
            "Length of longest substring containing '1' = "
            + maxlength(s));
    }
}


Python3
# Python program for the above approach
 
# Function to find length of
# longest substring containing '1'
def maxlength(s):
    n = len(s)
    ans = 0;
    for i in range(n):
         
        # Count the number of contiguous 1's
        if (s[i] == '1'):
            count = 1
            j = i + 1
            while(j <= n - 1 and s[j] == '1'):
                count += 1
                j += 1
            ans = max(ans, count)
    return ans
 
# Driver Code
s = "11101110";
print(maxlength(s))
 
# This code is contributed by Shivani


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find length of
// of longest substring containing '1'
static int maxlength(String s)
{
    int n = s.Length, i, j;
    int ans = 0;
     
    for(i = 0; i <= n - 1; i++)
    {
         
        // Count the number of contiguous 1's
        if (s[i] == '1')
        {
            int count = 1;
            for(j = i + 1;
                j <= n - 1 && s[j] == '1';
                j++)
                count++;
                 
            ans = Math.Max(ans, count);
        }
    }
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "11101110";
 
    Console.Write(maxlength(s));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
3

时间复杂度: O(N 2 )
辅助空间: O(1)