ø€ËγнÏ
Try it online or verify all test cases.
Or alternatively:
ηR.ΔÅ?
Might be invalid, since it outputs -1
instead of an empty string if the strings have no common prefix.
Try it online or verify all test cases.
Explanation:
ø # Zip the two (implicit) inputs together, creating pairs
# i.e. "abc1de98f" and "abc2de87f"
# → ["aa","bb","cc","12","dd","ee","98","87","ff"]
€Ë # Check for each if all inner items (the two characters in this case) are the same
# → [1,1,1,0,1,1,1,0,0,1]
γ # Split it into parts, grouping the same subsequent values together
# → [[1,1,1],[0],[1,1],[0,0],[1]]
н # Pop and only leave the first item
# → [1,1,1]
Ï # Only leave the characters at the truthy indices in the (implicit second) input
# i.e. "abc2de87f" and [1,1,1] → "abc"
# (after which the result is output implicitly)
η # Get the prefixes of the first (implicit) input
# i.e. "abc1de98f"
# → ["a","ab","abc","abc1","abc1d","abc1de","abc1de9","abc1de98","abc1de98f"]
R # Reverse this list
# → ["abc1de98f","abc1de98","abc1de9","abc1de","abc1d","abc1","abc","ab","a"]
.Δ # Then find the first item in this list which is truthy for:
# (which will result in -1 if none are found)
Å? # Check if the second (implicit) input starts with the current prefix
# i.e. "abc2de87f" and "abc1d" → 0 (falsey)
# i.e. "abc2de87f" and "abc" → 1 (truthy)
# (after which the result is output implicitly)
global
andGLOSSARY
returnglo
or''
? \$\endgroup\$