comments | difficulty | edit_url | rating | source | tags | |||
---|---|---|---|---|---|---|---|---|
true |
Medium |
1656 |
Biweekly Contest 75 Q3 |
|
You are given a 0-indexed binary string s
which represents the types of buildings along a street where:
s[i] = '0'
denotes that theith
building is an office ands[i] = '1'
denotes that theith
building is a restaurant.
As a city official, you would like to select 3 buildings for random inspection. However, to ensure variety, no two consecutive buildings out of the selected buildings can be of the same type.
- For example, given
s = "001101"
, we cannot select the1st
,3rd
, and5th
buildings as that would form"011"
which is not allowed due to having two consecutive buildings of the same type.
Return the number of valid ways to select 3 buildings.
Example 1:
Input: s = "001101" Output: 6 Explanation: The following sets of indices selected are valid: - [0,2,4] from "001101" forms "010" - [0,3,4] from "001101" forms "010" - [1,2,4] from "001101" forms "010" - [1,3,4] from "001101" forms "010" - [2,4,5] from "001101" forms "101" - [3,4,5] from "001101" forms "101" No other selection is valid. Thus, there are 6 total ways.
Example 2:
Input: s = "11100" Output: 0 Explanation: It can be shown that there are no valid selections.
Constraints:
3 <= s.length <= 105
s[i]
is either'0'
or'1'
.
According to the problem description, we need to choose
We can enumerate the middle building, assuming it is
The time complexity is
class Solution:
def numberOfWays(self, s: str) -> int:
l = [0, 0]
r = [s.count("0"), s.count("1")]
ans = 0
for x in map(int, s):
r[x] -= 1
ans += l[x ^ 1] * r[x ^ 1]
l[x] += 1
return ans
class Solution {
public long numberOfWays(String s) {
int n = s.length();
int[] l = new int[2];
int[] r = new int[2];
for (int i = 0; i < n; ++i) {
r[s.charAt(i) - '0']++;
}
long ans = 0;
for (int i = 0; i < n; ++i) {
int x = s.charAt(i) - '0';
r[x]--;
ans += 1L * l[x ^ 1] * r[x ^ 1];
l[x]++;
}
return ans;
}
}
class Solution {
public:
long long numberOfWays(string s) {
int n = s.size();
int l[2]{};
int r[2]{};
r[0] = ranges::count(s, '0');
r[1] = n - r[0];
long long ans = 0;
for (int i = 0; i < n; ++i) {
int x = s[i] - '0';
r[x]--;
ans += 1LL * l[x ^ 1] * r[x ^ 1];
l[x]++;
}
return ans;
}
};
func numberOfWays(s string) (ans int64) {
n := len(s)
l := [2]int{}
r := [2]int{}
r[0] = strings.Count(s, "0")
r[1] = n - r[0]
for _, c := range s {
x := int(c - '0')
r[x]--
ans += int64(l[x^1] * r[x^1])
l[x]++
}
return
}
function numberOfWays(s: string): number {
const n = s.length;
const l: number[] = [0, 0];
const r: number[] = [s.split('').filter(c => c === '0').length, 0];
r[1] = n - r[0];
let ans: number = 0;
for (const c of s) {
const x = c === '0' ? 0 : 1;
r[x]--;
ans += l[x ^ 1] * r[x ^ 1];
l[x]++;
}
return ans;
}