We need to compute minimal and maximal possible Hamming distance between two strings S1 and S2 after replacing each '?' with any lowercase Latin letter (a-z). Both strings have equal length (given that they have equal lengths, but constraints say |S1|,|S2| ≤ 100; however it's stated originally they have equal lengths; we'll assume they are equal per test case).
We need to output minimal and maximal Hamming distance.
We can think of each position independently? For maximal distance, we want as many positions as possible where S1i != S2i. For minimal, we want as many equal as possible.
But note that '?' can be assigned arbitrarily. So for each position, we have three cases:
- Both chars known (not '?'): Then it's fixed: either equal (contribute 0 to Hamming distance) or not equal (contribute 1). No choice.
- One known, one '?': We can choose the '?' to make equal or not equal, because we can pick any letter. So for minimal we can set it equal (choose same as known), contributing 0; for maximal we can set it not equal (choose a different letter), contributing 1. However, careful: can we always set it different? Yes, because alphabet has at least 2 letters, so we can pick a letter different from the known one. So for maximal, we can always make them differ. So contribution: min: 0, max: 1.
- Both '?': Then we can choose both letters. For minimal, we can choose same letter, so 0; for maximal, we can choose different letters, so 1. So again min: 0, max: 1.
Wait: That suggests that the maximal distance is simply the total number of positions where it's possible to make them differ? But check constraints: For both known and not equal, they are forced to differ, so that's 1 in both min and max? Actually, if both known and not equal, that contributes 1 to Hamming distance regardless of our choices (since we cannot change known letters). So for those positions, min and max both have 1. For positions with one or both '?', min contribution 0, max contribution 1. So then minimal total = number of positions where known and not equal? But also we might have some positions where both known and equal: that's 0 forced. For min, we would like to minimize distance, so we set '?' appropriately to avoid differences. So indeed min = number of positions where both known and differ (i.e., S1[i] != S2[i] and both not '?') because we can't change those. For positions with at least one '?', we can make them equal (choose '?' accordingly) to achieve 0. But is there any scenario where we might be forced to have a difference due to conflicting assignments? For instance, if we have two positions that somehow interact? No, each position independent. So yes, minimal = count of forced mismatches (both known and different). However, is there any subtlety where we could reduce forced mismatches by changing known letters? No, we cannot change known letters. So minimal is forced mismatches.
Similarly, maximal = number of positions where we can force a mismatch. That includes forced mismatches (both known and differ) which are forced 1, plus all positions where we have at least one '?' because we can set them to differ. But careful: For positions where both known and equal (forced equal), we cannot change them; they are 0. So maximal = forced mismatches (both known and differ) + count of positions with at least one '?'? But in positions where both known and equal, they are fixed 0. So indeed:
Let N = length of strings.
Define:
- forced_mismatch: both chars are known and different.
- forced_match: both known and equal.
- one_question: exactly one of the two is '?'.
- both_question: both are '?'.
Then:
min = forced_mismatch (since we can avoid all other mismatches)
max = forced_mismatch + one_question + both_question (since we can make all those positions mismatch). But also forced_mismatch are already mismatches, so they contribute 1 each. The sum forced_mismatch + one_question + both_question = total positions minus forced_match. Because total positions = forced_match + forced_mismatch + one_question + both_question. So max = N - forced_match. That matches intuition: We can make all non-forced-match positions differ, and forced matches are always matches.
But wait: In positions with one '?' we can always make them differ? Yes, because we can set the '?' to a letter different from the known letter. Since alphabet size >= 2, we can pick a different letter. So okay.
In both '?' we can set different letters. So yes.
Thus solution seems trivial: compute forced_match (both known and equal) and forced_mismatch (both known and not equal) and others. Then min = forced_mismatch; max = N - forced_match.
But let's verify with examples.
Example 1:
S1 = a?c
S2 = ??b
Length N=3.
Positions:
1: a vs ? -> one_question => can be match or mismatch.
2: ? vs ? -> both_question
3: c vs b -> forced_mismatch
forced_match = 0; forced_mismatch = 1; one_question = 1; both_question = 1.
Min = forced_mismatch = 1. Max = N - forced_match = 3 - 0 = 3. Matches output "1 3".
Example 2:
S1 = ???a
S2 = ???a
N=4.
Positions:
1: ? vs ? -> both_question
2: ? vs ? -> both_question
3: ? vs ? -> both_question
4: a vs a -> forced_match
forced_match=1, forced_mismatch=0, one_question=0, both_question=3.
Min = forced_mismatch = 0. Max = N - forced_match = 4-1=3. Output "0 3". Matches.
Example 3:
S1 = ?abac
S2 = aba?w
Let's align length: "?abac" length 5, "aba?w" length 5.
Positions:
1: ? vs a -> one_question (S1? , S2 a)
2: a vs b -> forced_mismatch (a vs b)
3: b vs a -> forced_mismatch (b vs a)
4: a vs ? -> one_question (a vs ?)
5: c vs w -> forced_mismatch (c vs w)
So forced_match = 0; forced_mismatch = 3; one_question = 2; both_question = 0.
Min = forced_mismatch = 3; Max = N - forced_match = 5 - 0 = 5. But output given is "3 5"? Actually example says output: 3 5. Wait check: forced_mismatch count: positions 2,3,5 are mismatches, yes 3. Max = total N =5 because forced_match=0, so max=5. Output is 3 5. So matches.
Thus solution is correct.
But is there any hidden nuance? Possibly constraints: strings have equal lengths but there is no explicit guarantee that they are equal; but problem states originally contained strings of equal lengths. So we can assume.
Edge cases: When both strings are of length 1, etc. Works.
Thus algorithm for each test case: read S1, S2, compute length n. Initialize forced_match = 0, forced_mismatch = 0 (or simply min = count of known and different). Iterate over positions i from 0 to n-1:
- if S1[i] != '?' and S2[i] != '?':
- if S1[i] == S2[i]: forced_match++ else forced_mismatch++
- else: nothing else needed for max/min directly? Actually for max we need count of positions that are not forced_match. So we can compute max = n - forced_match. Min = forced_mismatch.
But we could also compute both directly: min = number of positions where both known and differ. max = number of positions where either both known and differ, or at least one unknown. That is n - number of positions where both known and equal.
So we can just compute count_eq_known (both known and equal) and count_diff_known (both known and different). Then min = count_diff_known. max = n - count_eq_known.
Yes.
Check for any scenario where we might be able to achieve a higher max by possibly aligning '?' to create mismatches across positions? No, because each position independent.
Thus it's trivial.
But wait, I recall a known CodeChef problem "CHEFSTR1"? No, this is "CHEFARC"? Actually the problem seems to be from CodeChef: "Minimum Maximum Difference" maybe. I think it's correct.
But let's double-check: Are there any constraints that could make maximal not simply n - forced_match? Suppose both '?', we can set them different; yes. Suppose one '?', we can set to a different letter; but we must consider that if we want to maximize overall distance, we might want to set some '?' in one position to match the other string's letter, but that would reduce distance, not increase. So we want them to be different. So we set them all to differ.
But is there any scenario where setting one '?' to differ might conflict with setting another '?' to differ? No.
Thus max is simply count of positions that are not forced matches. That's correct.
But could we ever get more mismatches than n? No.
Thus code is straightforward.
Implementation: Read integer T. For each test case, read two strings (may have newline). Compute n = s1.length() (or s2.length()?). Should be same. Then loop i, if s1[i] != '?' and s2[i] != '?' then if s1[i] == s2[i] increment eq else increment diff. Then min = diff; max = n - eq. Print min and max.
Let's test on a few cases:
Case: S1 = a, S2 = a => eq=1, diff=0, n=1 => min=0, max=0 => correct.
Case: S1 = a, S2 = b => eq=0, diff=1 => min=1, max=1 => correct.
Case: S1 = ?, S2 = a => eq=0? Since one is '?', not both known, so eq count not increased. So eq=0, diff=0 => min=0, max=1 => we can choose '?' to match (0) or differ (1). Correct.
Case: S1 = ?, S2 = ? => eq=0, diff=0 => min=0, max=1 (since we can choose same or different). correct.
Thus algorithm works.
But wait, what about if both strings contain only letters but we can change '?'; but maybe there is a subtlety: In minimal difference, could there be a scenario where we want to make a '?' different from the known to avoid creating a mismatch elsewhere? No, because positions independent.
So it's done.
Hence solution.
Now we need to output T lines.
Complexities: O(total length) per test case.
We should write code accordingly.
But as we need to respond with final answer, we can provide Python code.
But the instruction: "We need to output minimal and maximal difference." We'll produce code.
But is there any scenario where we cannot assign both '?' to different letters because of some global constraint? No, each '?' can be assigned independently.
Thus final answer: Code as described.
I'll include reading input, maybe using sys.stdin.
But the user asked for the solution? The ChatGPT is supposed to produce answer? The instruction: "Chef has found two very old sheets... Go on, help him!" So likely they want the solution. However as ChatGPT, I must respond with explanation and code? The conversation might be from a platform where user expects answer. Usually I'd provide explanation and then code. But since the message says "You are ChatGPT, a large language model...", and the user input includes the problem statement, they likely want solution. I'll provide a brief explanation and then the Python code.
Nevertheless, I must check constraints: T ≤ 100, length ≤ 100, trivial.
I'll write code:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
T = int(next(it))
out = []
for _ in range(T):
s1 = next(it)
s2 = next(it)
n = len(s1) # assume equal
eq = 0
diff = 0
for c1, c2 in zip(s1, s2):
if c1 != '?' and c2 != '?':
if c1 == c2:
eq += 1
else:
diff += 1
min_diff = diff
max_diff = n - eq
out.append(f"{min_diff} {max_diff}")
sys.stdout.write("\n".join(out))
if name == "main":
solve()
That's it.
But wait, check for a potential trap: The strings may have different lengths? The problem statement says "the strings on both the sheets have equal lengths." So ok.
But sometimes CodeChef problem "Minimum Maximum" has additional nuance? Actually I recall a CodeChef problem "Minimum Maximum" maybe but there is also "Minimum Difference" but anyway.
Thus answer is as above.
I'll produce a final message with explanation and code. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
s1 = next(it)
s2 = next(it)
n = len(s1) # both strings have the same length
forced_eq = 0
forced_diff = 0
for c1, c2 in zip(s1, s2):
if c1 != '?' and c2 != '?':
if c1 == c2:
forced_eq += 1
else:
forced_diff += 1
minimum = forced_diff
maximum = n - forced_eq
out_lines.append(f"{minimum} {maximum}")
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()