Two background services, A and B, each produce a log of events encoded as a string. A monitoring pipeline merges the two logs into a single combined log by interleaving their characters: every character of A and every character of B must appear exactly once in the merged log, and each source's own characters must keep their original relative order. Additionally, because of a fairness rule, the merged log may never contain more than k consecutive characters taken from the same source in a row.
Given k, the two source logs, and a candidate merged log, determine whether the candidate could actually have been produced this way.
Line 1: an integer k.
Line 2: the log from service A (may be empty, producing a blank line).
Line 3: the log from service B (may be empty, producing a blank line).
Line 4: the candidate merged log (may be empty, producing a blank line).
All logs contain only lowercase English letters.
Print 1 if the candidate log can be produced by interleaving the two source logs (preserving each source's internal order) with no run of more than k consecutive characters from the same source, and 0 otherwise.
Example 1
Input
2 ab cd acbd
Expected
1
Explanation
Taking 'a' (A), 'c' (B), 'b' (A), 'd' (B) preserves both sources' internal order and never repeats the same source more than once in a row, which is within the limit k=2, so the answer is 1.
Example 2
Input
1 aa b aab
Expected
0
Explanation
Producing "aab" would require taking both 'a' characters from A back-to-back, a run of length 2, which exceeds the limit k=1, so the answer is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →