cswork / OAOpen practice workspace

MicrosoftOA #49

Minimum Replacements Required to Make a Matrix Balanced

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyHashing

Replace some ? with R or W; the remaining ? may be left as they are. Every row and every column must have equal numbers of R and W. Find the minimum number of replacements, or output −1 if it is impossible.

This site uses a standard input/output protocol.

Input

The first line contains N, followed by two strings of length N over R/W/?. This site uses 1≤N≤100000.

Output

Output a single integer, the answer.

Example 1

Input:

5
WR???
R???W

Output:

4

Example 2

Input:

1
R
R

Output:

-1

Example 3

Input:

2
??
??

Output:

0

Example notes

Example 1: first fill the two single ? cells, then use one double-? column to balance the row counts, for 4 replacements. Example 2: a fixed same-color column can never be balanced, so −1. Example 3: with all ?, both colors count 0 and are already balanced, so 0 replacements.

Editorial

Loading solution…