Longest Substring of 1 's after removing one character | Java 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
import java . util . * ; class GFG { static int longestSubarray ( String s ) { int i = 0 , j = 0 , k = 1 ; for ( j = 0 ; j < s . length ( ) ; ++ j ) { if ( s . charAt ( j ) == '0' ) k -- ; if ( k < 0 && s . charAt ( i ++ ) == '0' ) k ++ ; } return j - i - 1 ; } public static void main ( String [ ] args ) { String S = "011101101" ; System . out . print ( longestSubarray ( S ) ) ; } }