Count of substrings of a Binary string containing only 1 s | Java implementation to find count of substring containing only ones ; Function to find the total number of substring having only ones ; Driver code
class GFG { static int countOfSubstringWithOnlyOnes ( String s ) { int res = 0 , count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { count = s . charAt ( i ) == '1' ? count + 1 : 0 ; res = ( res + count ) ; } return res ; } public static void main ( String [ ] args ) { String s = "0110111" ; System . out . println ( countOfSubstringWithOnlyOnes ( s ) ) ; } }