Reputation: 5825
Let's say I have two strings:
"hello"
"love"
The size of the maximum subarray in the strings is 2: "lo".
Here's another example:
"ABBABBA"
"BBABCBA"
Maximum subarray: "BBAB"
Size: 4
Basically, how can I solve this problem in the most efficient way?
My idea is the following:
But I think this is some bad-looking brute force. Any idea of how I can improve this?
Thank you!
EDIT I'll need the string too.
Upvotes: 1
Views: 226
Reputation: 726489
This is a well-known problem called Longest Common Substring. It can be solved in O(mn)
, where m
and n
are lengths of the individual strings, using dynamic programming approach. The article in Wikipedia contains easy-to-follow pseudocode.
Upvotes: 5