696. Count Binary Substrings

Description

Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively.

Substrings that occur multiple times are counted the number of times they occur.

Example 1:

Input: "00110011"
Output: 6
Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".

Notice that some of these substrings repeat and are counted the number of times they occur.

Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together.

Example 2:

Input: "10101"
Output: 4
Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.

Note:

  • s.length will be between 1 and 50,000.*
  • s will only consist of "0" or "1" characters.

Solution

Slide window, time O(n), space O(1)

初始的想法就是找到两个元素不同的位置,然后往两边扩散。

class Solution {
    public int countBinarySubstrings(String s) {
        if (s == null || s.length() < 2) {
            return 0;
        }
        
        char[] arr = s.toCharArray();
        int i = 0;
        int len = s.length();
        int count = 0;
        while (i < len - 1) {
            if (arr[i] == arr[i + 1]) {
                ++i;
                continue;
            }
            
            ++count;
            int j = i + 1;
            while (i > 0 && j < len - 1 && arr[i - 1] == arr[i] && arr[j + 1] == arr[j]) {
                ++count;
                --i;
                ++j;
            }
            
            i = j;
        }
        
        return count;
    }
}

time O(n), space O(1)

这是一个蛮有意思的解法

  • preRun count the same item happend before (let say you have 0011, preRun = 2 when you hit the first 1, means there are two zeros before first ‘1’)
  • curRun count the current number of items (let say you have 0011, curRun = 2 when you hit the second 1, means there are two 1s so far)
  • Whenever item change (from 0 to 1 or from 1 to 0), preRun change to curRun, reset curRun to 1 (store the curRun number into PreRun, reset curRun)
  • Every time preRun >= curRun means there are more 0s before 1s, so could do count++ . (This was the tricky one, ex. 0011 when you hit the first ‘1’, curRun = 1, preRun = 2, means 0s number is larger than 1s number, so we could form “01” at this time, count++ . When you hit the second ‘1’, curRun = 2, preRun = 2, means 0s’ number equals to 1s’ number, so we could form “0011” at this time, that is why count++)
class Solution {
    public int countBinarySubstrings(String s) {
        if (s == null || s.length() < 2) {
            return 0;
        }
        
        int preRun = 0;
        int curRun = 1;
        int count = 0;
        
        for (int i = 1; i < s.length(); ++i) {
            if (s.charAt(i) == s.charAt(i - 1)) {
                ++curRun;
            } else {
                preRun = curRun;
                curRun = 1;
            }
            
            if (preRun >= curRun) {
                ++count;
            }            
        }
        
        return count;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,496评论 0 23
  • 合同到期之后,需从原单位搬离出去,我不得不再次和房东打交道。房东者流,在我看来,多有副冷漠的嘴脸。这使我在找房的时...
    秋水斋主人阅读 8,086评论 1 7
  • 你伴我走过旅程的一站, 又默默地抓紧离别的瞬间 眼睛问着眼睛 问出流溢的情感 没有言语再见 没有泪眼涟连 只有紧扣...
    六瓣丁香阅读 1,135评论 3 1
  • 前些天,某读书群里的书友艾草分享了“HDD室内设计”(一微信公众号的名字)里一篇名为《花100万他就开了家小店,不...
    谷雨一一阅读 4,249评论 1 1

友情链接更多精彩内容