Great solution to the problem. I think there might be several improvements though.
First find which string is longer and start with the shorter strings length.
Dependending on the string length, this solution could take very very long. On the plus side you can stop on the first match, but especially when assuming that the longest common substring will be a combination of several words or a short sentence, you'll be comparing stuff for a long time on longer texts. This could be massively sped up by choosing a different approach on this. For example
Start with half the length of the shorter string. If there is a match raise the length by 50%. If there is not, divide the length by 2. This way you'll approach the string length way quicker, especially on long strings.
Start at 1. Search for the first letter appearing in both. When found, raise length.
Search for first pair of letters appearing in both. When found one, raise length. And so on. This will also be much quicker on long strings.
Love the challenge, I'll create an alternative and benchmark this later...
This seems to outperform the posted solution in most cases (by a huge factor up to 500 or more). For the extreme examples of having one very long string and one very short, both solutions seem to be pretty close, depending on the string order. While the order can easily be fixed, this still is way quicker in most "normal" circumstances.
Let me know if you find even better solutions. :-)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Great solution to the problem. I think there might be several improvements though.
Love the challenge, I'll create an alternative and benchmark this later...
So, I played around a bit, sadly didn't have time for deeper look and a lot of tries. Here is my quick solution to this:
This seems to outperform the posted solution in most cases (by a huge factor up to 500 or more). For the extreme examples of having one very long string and one very short, both solutions seem to be pretty close, depending on the string order. While the order can easily be fixed, this still is way quicker in most "normal" circumstances.
Let me know if you find even better solutions. :-)