Three Strings
Practice this coding question on Coding Gurukul.
Problem statement
Two archive scanners produce character streams a and b. A merger takes the next unread character from either scanner until both streams are exhausted, preserving the order within each stream. Some characters in the merged record were later overwritten, and only the damaged record c remains. Your task Find the smallest number of character replacements needed to make c a valid interleaving of a and b. Its length is already equal to the sum of their lengths. Important details Use every source character exactly once. Replacements affect c only; insertions, deletions and reordering inside a or b are forbidden. A zero answer means c is already a valid interleaving. A locally matching choice of stream may prevent a better match later. Worked example With a = ab, b = cd and c = axbd, the valid merge acbd differs only at its second character. One replacement suffices, while zero is impossible because x appears in neither stream, so the answer is 1. With a = a, b = a and c = aa, no replacement is needed.
Input format
Read t. For each case read a, b and c on separate lines.
Output format
Print the minimum replacements for each case.
Constraints
1 <= t <= 20; 1 <= length(a), length(b) <= 300; length(c) = length(a)+length(b); total length(a)+length(b) across cases <= 1000; lowercase English letters only.
Sample input
1 ab cd axbd
Sample output
1
Sign in to run or submit a solution in C++, Java or Python.