To count all 1 's in the string
int cnt_one = 0 ; int max_cnt = 0 , temp = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' ) { cnt_one ++ ; temp ++ ; } else { max_cnt = Math . max ( max_cnt , temp ) ; temp = 0 ; } } max_cnt = Math . max ( max_cnt , temp ) ;