The twin keys
- O(n) · Medium
- Full plan
- Python
- JavaScript
- strings
- loops
- indexes
Problem
Nobody can find the key ring for the north wing of Ravel Manor, so the locksmith works with the loose keys that turned up in the drawers. Each key is written as a string of digits, one per tooth, from the bow to the tip: "3152" has four teeth.
Write a function that takes two keys (strings, which can be empty) and returns, as a string, the stretch they share starting from the bow. With "31527" and "31587" you return "315": on the fourth tooth one has 2 and the other 8, and whatever comes after no longer counts, even though the last tooth matches. If one key is shorter and the other starts the same way, the stretch is the whole short key. If the first tooth is already different, or one of the keys is empty, return "".
Examples
The example
"31527", "31587" → "315"
Identical keys
"4455", "4455" → "4455"
The first one is shorter
"123", "12345" → "123"
The second one is shorter
"98765", "98" → "98"
Different from the first tooth
"52", "25" → ""
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def shared_start(first, second):
passJavaScript
function sharedStart(first, second) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.