A regional distribution center stamps every incoming bin with a short alphabetic tag before it goes onto the loading manifest. A tag is considered "absorbed" whenever it appears as a contiguous run of characters inside some other bin's tag on the same manifest. Given the manifest, list every absorbed tag, in the order it appears on the manifest.
Print, one per line and in the same relative order as the manifest, every tag that occurs as a contiguous substring of at least one other tag on the manifest. If no tag qualifies, print nothing (an empty output, aside from an optional trailing newline, is acceptable).
Example 1
Input
4 cargo car go freight
Expected
car go
Explanation
"car" and "go" both occur as contiguous substrings inside "cargo", so both are absorbed. "cargo" itself is not contained in any other tag, and "freight" is not contained in any other tag either. In manifest order, the absorbed tags are "car" then "go".
Example 2
Input
3 alpha beta gamma
Expected
(empty)Explanation
No tag is a contiguous substring of another tag here ("alpha", "beta", and "gamma" share no such containment relationship), so no tag qualifies and the output is empty.
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 →