温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

LeetCode中计数二进制子串的示例分析

发布时间:2021-09-18 13:49:28 来源:亿速云 阅读:102 作者:柒染 栏目:编程语言

这期内容当中小编将会给大家带来有关LeetCode中计数二进制子串的示例分析,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

Description

https://leetcode.com/problems/count-binary-substrings/

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.

Analysis

方法一:暴力算法,遇到超长字符串会超时,故弃。

方法二:发现字符串其中规律

  1. 如'00000111',一个连续'0'与连续'1'的字符串,符合题意要求的子字符串为min('0‘的个数, '1'的个数)=min(5,3)=3。

  2. 如’000011100111‘字符串,以连续相同字符分为一个区间规则,得到[0,3],[4,6],[7,8],[9,11]共4个区间。

  3. 由2. 得到区间,将其两两相邻配对,如[0,3]与[4,6],[4,6]与[7,8],再由1. 得出规律求出两配对区间符合题意要求的子字符串个数,最后将其累加,便能得到最后结果。

方法三:方法二改进版。

Submission

import java.util.ArrayList;
import java.util.List;

public class CountBinarySubstrings {

	// 方法一:暴力算法,遇到超长字符串会超时
	public int countBinarySubstrings(String s) {
		int result = 0;
		// List<String> resultList = new ArrayList<>();
		for (int i = 0; i < s.length(); i++) {
			int state = 1;
			char tempChar = s.charAt(i);
			int firstCharCount = 1, secondCharCount = 0, rightPartCount = 0;
			for (int j = i + 1; j < s.length(); j++) {
				if (state == 1) {

					if (tempChar == s.charAt(j)) {
						firstCharCount++;
					} else {
						state = 2;
						tempChar = s.charAt(j);
					}
				}

				if (state == 2) {
					if (tempChar == s.charAt(j)) {
						secondCharCount++;
					}
					rightPartCount++;

					if (rightPartCount == firstCharCount) {
						if (secondCharCount == firstCharCount) {
							result++;
							// resultList.add(repeat(tempChar=='1'?'0':'1', firstCharCount) +
							// repeat(tempChar, secondCharCount));
						}
						break;
					}
				}
			}
		}

		return result;
	}

	// 方法二:
	public int countBinarySubstrings2(String s) {
		List<int[]> list = new ArrayList<>();
		int result = 0, tempIndex = 0;
		for (int i = tempIndex + 1; i <= s.length(); i++) {
			if (i == s.length() || s.charAt(i) != s.charAt(tempIndex)) {
				list.add(new int[] { tempIndex, i - 1 });
				tempIndex = i;
			}
		}

		for (int i = 0; i < list.size(); i++) {
			int[] leftPart = list.get(i);
			if (i + 1 == list.size()) {
				break;
			}
			int[] rightPart = list.get(i + 1);

			int leftSize = leftPart[1] - leftPart[0] + 1;
			int rightSize = rightPart[1] - rightPart[0] + 1;

			result += Math.min(leftSize, rightSize);
		}
		return result;
	}

	// 方法三:方法二的改进版
	public int countBinarySubstrings3(String s) {
		int result = 0, lastIndex = 0, lastSize = 0;
		for (int i = lastIndex + 1; i <= s.length(); i++) {

			if (i == s.length() || s.charAt(i) != s.charAt(lastIndex)) {

				if (lastSize == 0) {
					lastSize = i - lastIndex;
				} else {
					int currentSize = i - lastIndex;
					result += Math.min(lastSize, currentSize);
					lastSize = currentSize;
				}
				lastIndex = i;
			}

		}

		return result;
	}

	private String repeat(char c, int times) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < times; i++) {
			sb.append(c);
		}
		return sb.toString();
	}

}

Test

import static org.junit.Assert.*;
import org.junit.Test;

public class CountBinarySubstringsTest {

	@Test
	public void test() {
		CountBinarySubstrings obj = new CountBinarySubstrings();

		assertEquals(6, obj.countBinarySubstrings("00110011"));
		assertEquals(4, obj.countBinarySubstrings("10101"));
	}
	
	@Test
	public void test2() {
		CountBinarySubstrings obj = new CountBinarySubstrings();
		
		assertEquals(6, obj.countBinarySubstrings2("00110011"));
		assertEquals(4, obj.countBinarySubstrings2("10101"));
	}
	
	@Test
	public void test3() {
		CountBinarySubstrings obj = new CountBinarySubstrings();
		
		assertEquals(0, obj.countBinarySubstrings3("00"));
		assertEquals(1, obj.countBinarySubstrings3("001"));
		assertEquals(6, obj.countBinarySubstrings3("00110011"));
		assertEquals(4, obj.countBinarySubstrings3("10101"));
		assertEquals(3, obj.countBinarySubstrings3("00110"));
	}
}

上述就是小编为大家分享的LeetCode中计数二进制子串的示例分析了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI