Consider the following array:
/www/htdocs/1/sites/lib/abcdedd /www/htdocs/1/sites/conf/xyz /www/htdocs/1/sites/conf/abc/def /www/htdocs/1/sites/htdocs/xyz /www/htdocs/1/sites/lib2/abcdedd
what is the shortest and most elegant way of detecting the common base path - in this case
/www/htdocs/1/sites/
and removing it from all elements in the array?
lib/abcdedd conf/xyz conf/abc/def htdocs/xyz lib2/abcdedd
Write a function longest_common_prefix
that takes two strings as input. Then apply it to the strings in any order to reduce them to their common prefix. Since it is associative and commutative the order doesn't matter for the result.
This is the same as for other binary operations like for example addition or greatest common divisor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With