Longest Substring of 1 's after removing one character | C ++ Program to implement the above approach ; Function to calculate the length of the longest substring of '1' s that can be obtained by deleting one character ; Initializing i and j as left and right boundaries of sliding window ; If current character is '0' ; Decrement k by one ; If k is less than zero and character at ith index is '0' ; Return result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubarray ( string s ) { int i = 0 , j = 0 , k = 1 ; for ( j = 0 ; j < s . size ( ) ; ++ j ) { if ( s [ j ] == '0' ) k -- ; if ( k < 0 && s [ i ++ ] == '0' ) k ++ ; } return j - i - 1 ; } int main ( ) { string S = "011101101" ; cout << longestSubarray ( S ) ; return 0 ; }