Three field radios are each assigned a call-sign — a non-empty string of lowercase letters. A radio's call-sign can be shortened, one letter at a time, only by trimming its very last letter, and it must always keep at least one letter (a call-sign can never be trimmed away to nothing). The squad wants all three radios to end up broadcasting the exact same call-sign. Find the minimum total number of trims, counted across all three radios, needed to make the three call-signs identical, or report that no sequence of trims can ever make them identical.
s1s2s3Each line is a non-empty string of lowercase English letters — the current call-sign of one radio.
Print a single integer: the minimum total number of trims needed to make the three call-signs identical, or -1 if no sequence of trims can ever make them identical.
Example 1
Input
abc abb ab
Expected
2
Explanation
Comparing s1='abc', s2='abb', s3='ab' character by character: position 0 is 'a' in all three and position 1 is 'b' in all three, so the longest common prefix is 'ab' (length 2) — the comparison stops there because s3 has no position 2. Trimming s1 down to 'ab' costs 1 trim (removing its 'c'), trimming s2 down to 'ab' costs 1 trim (removing its final 'b'), and s3 is already 'ab' (0 trims). Total trims: 1 + 1 + 0 = 2.
Example 2
Input
dac bac cac
Expected
-1
Explanation
The first letters are 'd', 'b', and 'c' — three different letters. Since a call-sign can never be trimmed below length 1, the first letter of each string can never be removed, so the three call-signs can never become identical. The answer is -1.
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 →