R, 71 bytes (the function-only version: 49 bytes)
A recursive approach with regex:
s=scan(,"");`?`=\(a,b)`if`(regexpr(b,a)-1,a?sub(".$","",b),b);s[1]?s[2]
Attempt This Online!
- Checks if one string matches the beginning of another one.
- If true, the substring is output.
- Else remove the last character from the substring, pass to the function and call it again.
Matching substrings:
# R, 80 bytes
s=scan(,"");a=s[1];b=s[2];`+`=\(x)substring(x,1,1:nchar(a));max((+a)[+a==+b],"")
Attempt This Online!
Creates all possible sequential substrings that start from the 1st character for the both strings, subset for those that match and output the longest one.
An attempt to use a built-in abbreviate
# R, 48 bytes
sub(".$","",max(abbreviate(scan(,""),1,F,,,,F)))
Attempt This Online!
Note: unfortunately, this is not a valid answer, as it would fail if the provided strings contain a "
", "\t
" or similar. We assume that, if not otherwise specified, an input string might contain at least a space symbol.
global
andGLOSSARY
returnglo
or''
? \$\endgroup\$