Ruby: Time O(n) Space O(1)


  • 0
    S
    def count_binary_substrings(s)
      prev_count, curr_count, i, total, prev = 0, 1, 1, 0, s[0]
      for i in 1..s.size
        if s[i] == prev
          curr_count += 1
        else
          prev_count, curr_count, total = curr_count, 1, total + [ prev_count, curr_count ].min
        end
    
        prev = s[i]
      end
    
      total
    end
    

Log in to reply
 

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