Create a function to reverse string concatenation


Two strings (alphanumeric + spaces), where one should be subtracted for the other.

  • You can assume that the string to be subtracted will never be larger than the other one.


The result from the subtraction


You should remove one string from the start or the end of another string. If the string is present in the start and in the end, you can only remove one, which one will be removed is up to you.
If the string isn't in the start or in the end, or isn't an exact match, it is an invalid subtraction and you should output the original string.

Test Cases

Valid Subtraction

'abcde','ab' -> 'cde'
'abcde','cde' -> 'ab'
'abab','ab' -> 'ab'
'abcab','ab' -> 'abc' or 'cab'
'ababcde','ab' -> 'abcde'
'acdbcd','cd' -> 'acdb'
'abcde','abcde' -> ''
'abcde','' -> 'abcde'
'','' -> ''

Invalid Subtraction (returns original string)

'abcde','ae' -> 'abcde'
'abcde','aa' -> 'abcde'
'abcde','bcd' -> 'abcde'
'abcde','xab' -> 'abcde'
'abcde','yde' -> 'abcde'

Invalid Input (don't need to be handled)

'','a' -> ''

This is , so the shortest code in bytes wins!

  • 4
    \$\begingroup\$ Why is the result of the first case not cde? What do you mean by valid? Do we need to judge the validity of the input, or do you mean that we will not receive invalid inputs? \$\endgroup\$
    – Leaky Nun
    May 16, 2017 at 16:01
  • 7
    \$\begingroup\$ Damn you, 'abcde','bcd' -> 'abcde', for breaking my solution \$\endgroup\$ May 16, 2017 at 16:26
  • 5
    \$\begingroup\$ Can we assume the strings will be regex-safe (alphanumeric + spaces)? \$\endgroup\$ May 16, 2017 at 16:27
  • 2
    \$\begingroup\$ I'd suggest 'ababcde', 'ab''abcde' as a test case. Some naive algorithms fail on that one. \$\endgroup\$
    – user62131
    May 16, 2017 at 16:48
  • 2
    \$\begingroup\$ @Rod You might consider retitling the challenge "Reverse string concatenation"? \$\endgroup\$
    – MD XF
    May 16, 2017 at 19:52

32 Answers 32


Pip, 19 bytes


Try it online!

Kinda sad this isn't shorter. The main problem is that Pip's R operator replaces all matches, not just the first one; otherwise, aR"^|$"J Xbx would work for 12 bytes.

                     a, b are command-line args; x is empty string (implicit)
    X:b              Convert b to a regex in-place
 '^.                 Prepend ^
Y                    and yank that regex into the y variable
       aR         x  In a, replace the following expression with the empty string:
         yNa?         Is there a match of y in a? I.e., does b match at the beginning of a?
             y        If so, replace y (b at the beginning of a)
              b.'$    If not, replace b with $ appended (b at the end of a)

Kotlin, 62 42 56 bytes

+12 bytes I think that type definitions are mandatory in the Kotlin lambda with 2 arguments


Test script:

var f = {a:String,b:String->Regex("^$b|$b$").replaceFirst(a,"")}

fun main(args: Array<String>) {



Kotlin (without Regex), 62 76 bytes

{a:String,b:String->var c=a.removePrefix(b)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.