Skip to content

Commit 97870b8

Browse files
committed
Count Binary Substrings.
1 parent f4376c0 commit 97870b8

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
Given a binary string s, return the number of non-empty 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.
3+
4+
Substrings that occur multiple times are counted the number of times they occur.
5+
6+
7+
8+
Example 1:
9+
Input: s = "00110011"
10+
Output: 6
11+
Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".
12+
Notice that some of these substrings repeat and are counted the number of times they occur.
13+
Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together.
14+
15+
Example 2:
16+
Input: s = "10101"
17+
Output: 4
18+
Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.
19+
20+
21+
Constraints:
22+
- 1 <= s.length <= 105
23+
- s[i] is either '0' or '1'.
24+
*/
25+
class Solution {
26+
func countBinarySubstrings(_ s: String) -> Int {
27+
var arr = [String(s.first!)]
28+
for (i, c) in s.enumerated() {
29+
if i > 0 {
30+
let character = arr.last!.last!
31+
if c == character {
32+
var element = arr.popLast()!
33+
element.append("\(c)")
34+
arr.append(element)
35+
} else {
36+
arr.append("\(c)")
37+
}
38+
}
39+
}
40+
41+
var totalCount = 0
42+
for (i, e) in arr.enumerated() {
43+
if i < arr.count - 1 {
44+
let lastElement = arr[i + 1]
45+
totalCount += e.count >= lastElement.count ? lastElement.count : e.count
46+
}
47+
}
48+
return totalCount
49+
}
50+
}
51+
52+
let s = Solution()
53+
let r = s.countBinarySubstrings("11110011")
54+
print(r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/696.Count Binary Substrings.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
120. [Valid Palindrome II](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/680.Valid%20Palindrome%20II.playground/Contents.swift)
125125
121. [Baseball Game](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/682.Baseball%20Game.playground/Contents.swift)
126126
122. [Binary Number with Alternating Bits](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/693.Binary%20Number%20with%20Alternating%20Bits.playground/Contents.swift)
127+
123. [Count Binary Substrings](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/696.Count%20Binary%20Substringsplayground/Contents.swift)
127128

128129
#### Medium
129130

0 commit comments

Comments
 (0)