Objective
Create a function to reverse string concatenation
Input
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.
Output
The result from the subtraction
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 code-golf, so the shortest code in bytes wins!
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\$'abcde','bcd' -> 'abcde'
, for breaking my solution \$\endgroup\$'ababcde', 'ab'
→'abcde'
as a test case. Some naive algorithms fail on that one. \$\endgroup\$