Java very easy and concise O(n) time 18ms Solution(Beats 100% Java Submissions)


  • 0
    K
    class Solution {
        public int countBinarySubstrings(String s) {
            int len=s.length();
            if(len<=1) return 0;
            char[] sc= s.toCharArray();
            int i=0,prev=-1,res=0;
            while(i<len){
                int j=i;
                char c=sc[i];
                while(i<len && sc[i]==c) i++;
                int cur=i-j;
                if(prev!=-1) res+=Math.min(prev,cur);
                prev=cur;
            }
            return res;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.