Longest Substring containing '1' | C ++ program for the above approach ; Function to find length of longest substring containing '1' ; Count the number of contiguous 1 's ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxlength ( string s ) { int n = s . length ( ) , i , j ; int ans = 0 ; for ( i = 0 ; i <= n - 1 ; i ++ ) { 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 ; } int main ( ) { string s = "11101110" ; cout << maxlength ( s ) << endl ; return 0 ; }