Advertisement
vivek_ragi

Count_Binary_substrings

Jul 13th, 2021
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int countBinarySubstrings(string s) {
  4.         int n = s.size();
  5.         int i = 0, j = 0;
  6.         int cnt = 0, ans = 0;
  7.         while (i < n and j < n) {
  8.             if (s[i] == s[j]) {
  9.                 cnt++;
  10.                 j++;
  11.             }
  12.             else {
  13.                 cnt--;
  14.                 j++;
  15.             }
  16.            
  17.             if (cnt == 0) {
  18.                 ans++;
  19.                 i++;
  20.                 j = i;
  21.             }
  22.             if (j == n) {
  23.                 cnt = 0;
  24.                 i++;
  25.                 j = i;
  26.             }
  27.         }
  28.        
  29.         return ans;
  30.     }
  31. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement