Day 3: Parallel Dilemma

easyGreedyRecursion + PrecomputationPrefix Sum

You are given two parallel roads $$$A$$$ and $$$B$$$ of length $$$n$$$, each consisting of potholes and clear cells. You may choose at most one contiguous segment on each road to undergo construction. All potholes in those segments are removed, but the segments become blocked and cannot be used for travel.

You must travel from cell $$$1$$$ to cell $$$n$$$, starting on either road $$$A$$$ or $$$B$$$, moving only forward or switching roads at the same index — but you cannot enter blocked cells.

Choose your repair segments such that:

  • There remains at least one path from cell $$$1$$$ to cell $$$n$$$.
  • The total number of potholes removed is maximized.

Return the maximum number of potholes that can be removed.

Note: Choosing segments from both roads is optional. You may choose a segment from only one road or none at all.

Input Format:

The first line contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the length of the roads.

The second line contains a string of length $$$n$$$ representing the first road, consisting of characters . and X.

The third line contains a string of length $$$n$$$ representing the second road, consisting of characters . and X.

Output Format:

Print a single integer — the maximum number of potholes that can be removed such that a valid path from position $$$1$$$ to $$$n$$$ still exists.

Examples:

Example 1:

Input:

5
.....
.....

Output:

0

Example 2:

Input:

7
X.X.X.X
X.X.X.X

Output:

4

Example 3:

Input:

7
...XXXX
XXX....

Output:

6

Code

Loading Editor...