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
using System ; class GFG { static int longestSubarray ( string s ) { int i = 0 , j = 0 , k = 1 ; for ( j = 0 ; j < s . Length ; ++ j ) { if ( s [ j ] == '0' ) k -= 1 ; if ( k < 0 && s [ i ++ ] == '0' ) k ++ ; } return j - i - 1 ; } public static void Main ( string [ ] args ) { string S = "011101101" ; Console . Write ( longestSubarray ( S ) ) ; } }