Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Longest Substring containing ‘1’

  • Last Updated : 16 Aug, 2021

Given a binary string, the task is to print the length of the longest substring containing only ‘1’.

Examples:

Input: 110
Output: 2
Explanation: Length of the longest substring containing only ‘1’ is “11”.

Input: 11101110
Output: 3

Approach: Traverse the string and count the number of contiguous 1‘s encountered. Store the maximum number of contiguous 1s in a variable, say max. Finally, print the value of max obtained.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
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




<script>
 
        // JavaScript program for the above approach;
 
        // Function to find length of
        // longest substring containing '1'
        function maxlength(s)
        {
            let n = s.length, i, j;
            let ans = 0;
            for (i = 0; i <= n - 1; i++) {
 
                // Count the number of contiguous 1's
                if (s[i] == '1') {
 
                    let count = 1;
                    for (j = i + 1; j <= n - 1
                        && s[j] == '1';
                        j++)
                        count++;
                    ans = Math.max(ans, count);
                }
            }
            return ans;
        }
 
        // Driver Code
        let s = "11101110";
        document.write(maxlength(s));
 
// This code is contributed by Potta Lokesh
    </script>
Output: 
3

 

Time Complexity: O(N2)
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!