Asked in: DESHAW
#include <bits/stdc++.h>
using namespace std;
long countWays(const string& collection) {
// ... rest of solution available after purchase
```
To solve the problem of counting the number of ways to select three artworks from a binary string such that no two adjacent selected artworks have the same type, it's crucial to understand the pattern constraints and how to efficiently count valid triplets without brute force enumeration, which would be too slow for large input sizes.
Step 1: Understand the Problem and Constraints
You have a string of '0's and '1's representing traditional and modern artworks respectively. You want to choose indices i < j < k such that the sequence formed by these positions alternates between 0 and 1, i.e., no two adjacent selected artworks are identical. The length of the string can be as large as 200,000, so a naive cubic or quadratic solution is not feasible.
Step 2: Identify Possible Valid Patterns
Since you select three artworks in ascending order of indices, and adjacent selected artworks cannot be the same, the three selected artworks must follow either the pattern "0-1-0" or "1-0-1". These are the only two alternating triples possible. Any other combination will have at least two adjacent artworks of the same type.
Step 3: Translate the Problem to Counting Triplets Matching These Patterns
You want to count how many triplets (i, j, k) satisfy either pattern "0-1-0" or "1-0-1". For example, in "01001", the valid triplets are those with artworks in positions that form "010" or "101".
Step 4: Approach for Efficient Counting
Brute forcing all triples would be O(n^3), which is impractical. Instead, think about how to break down the counting using prefix and suffix counts or combinational logic.
You can iterate over each possible middle element j (the second chosen artwork), and count how many valid i (first artwork) and k (third artwork) positions exist to the left and right of j respectively that satisfy the pattern condition.
For instance, if the middle artwork at position j is '1', then the valid patterns would be "0-1-0", so you need to count the number of '0's to the left of j (possible i's) and the number of '0's to the right of j (possible k's). The total valid triplets with j as the middle element would be the product of these two counts.
Similarly, if the middle artwork at position j is '0', then the valid pattern is "1-0-1". You count how many '1's are to the left and right of j and multiply those counts.
Step 5: Preprocessing Counts for Efficiency
To achieve this, preprocess prefix counts for how many '0's and '1's appear up to each index, and suffix counts for how many '0's and '1's appear from each index to the end. This preprocessing takes O(n) time.
- Prefix counts: For each index, store how many '0's and '1's occur before or at that position.
- Suffix counts: For each index, store how many '0's and '1's occur after that position.
Step 6: Calculate the Number of Triplets
For each index j in the string:
- If collection[j] == '0', the number of valid triplets with j as the middle is:
(number of '1's before j) * (number of '1's after j)
- If collection[j] == '1', the number of valid triplets with j as the middle is:
(number of '0's before j) * (number of '0's after j)
Summing these values over all indices j gives the total number of valid triplets.
Step 7: Time and Space Complexity
This approach requires O(n) time to compute prefix and suffix arrays, and O(n) time to sum results for all possible middle elements, resulting in an overall O(n) time complexity. Space complexity is O(n) for prefix and suffix counts.
Step 8: Validate with Examples
For the sample "01001":
- Positions and values: 0:'0', 1:'1', 2:'0', 3:'0', 4:'1'
- Calculate prefix and suffix counts of '0's and '1's, then for each position count valid triplets.
- This yields the answer 4, matching the example.
Step 9: Edge Cases to Consider
- Strings with all '0's or all '1's where no triplets are possible.
- Very short strings with length less than 3, answer should be 0.
- Strings with alternating patterns ensuring maximal triplets.
Step 10: Summary
- Recognize only two valid triplet patterns: "0-1-0" and "1-0-1".
- Use prefix and suffix counts to efficiently calculate how many valid triplets have each index as the middle element.
- Sum results for all middle indices to get the total count.
- This method is efficient and scalable to large inputs, and does not require enumerating all triplets explicitly.
```