Leetcode - Count Binary Substrings (Python)

preview_player
Показать описание
April 2021 Leetcode Challenge
Leetcode - Count Binary Substrings #696
Difficulty: Easy
Рекомендации по теме
Комментарии
Автор

Your optimized solution shows no thought process but just memorization of optimal solution from others. Thought process should include how you capture block and increment accordingly

mdlwlmdddwd
Автор

what is the keyboard you are using? typing sound is very nice.

rosendo
Автор

I like the q solution, it reminds of trying to capture parentheses with a stack. I ended up using two pointer with this one after the hint XD

class Solution(object):
def countBinarySubstrings(self, s):
"""
:type s: str
:rtype: int
"""
count = 0
for i in range(len(s)):
#we need to scan for two cases 10 or 01
l, r = i, i+1
while l >=0 and r < len(s) and s[l] == '0' and s[r] == '1':
count += 1
l -= 1
r += 1
l, r = i, i+1
while l >=0 and r < len(s) and s[l] == '1' and s[r] == '0':
count += 1
l -= 1
r += 1
return count

janmichaelaustria
visit shbcf.ru