(all arrays and strings are based on 0 in this answer)
First, I want to indicate that the problem with the OP is confused. Assuming c is a common part of a and b , the OP input and output example assumes that c must be a suffix a and prefix b at the same time. I see that some of the answers above have accepted this understanding of the problem.
However, the original implementation provided by OP suggests that c can occur anywhere in a , being the prefix of b , because you are using a.Contains(auxString) . This means that for a=AAT and b=AAG your code will return AATG . However, other people's answers will return AATAAG .
So, there are two possible interpretations of your problem. Please clarify.
Secondly, assuming that the size of the first row a is N and the second row b is M , unlike the O(N*M) solution provided in the original solution and the existing answers, O(N+M) can be achieved using any of the following: KMP, Suffix Array, Suffix Tree, Z-algorithm.
I will briefly describe how to use the Z-algorithm to solve this problem here, since it seems to be much less mentioned in stackoverflow compared to others.
For details of the Z-algorithm, see http://www.cs.umd.edu/class/fall2011/cmsc858s/Lec02-zalg.pdf
Basically, for a string S length L it computes an array Z length L , in which Z[i] is equal to the longest common prefix S and S[i:] ( S[i:] means the substring S , starting at position i ).
For this task, we combine the rows a and b in d=b+a ( b before a ) and computes the array Z joined row d . Using this Z array, we can easily determine the longest prefix b , which also occurs in a .
For a possible interpretation of one of the problems in which c must be the suffix a and the prefix b :
max_prefix = 0 for i in range(M, N+M): if Z[i] == N+M - i: if Z[i] > max_prefix: max_prefix = Z[i]
and the answer will be as follows:
a+b[max_prefix:]
For a possible interpretation of two problems in which c must be the prefix b and can be anywhere in a :
max_prefix = 0 for i in range(M, N+M): if Z[i] > max_prefix: max_prefix = Z[i]
again the answer will be:
a+b[max_prefix:]
The difference in these two cases is as follows:
if Z[i] == N+Mi:
To understand this line, remember that Z[i] is the longest common prefix for the lines d and d[i:] , then:
- Note that
d=b+a - We list
i from M to M+N-1 , which is the range of a to d . So d[i:] is equal to a[iM:] . And the length a[iM:] is N-(iM)=N+Mi - Since
d starts with b , checking if Z[i] N+Mi is equal, whether a[iM:] checks the prefix b . If they are really equal, then we found the common string c , which is the prefix b , as well as the suffix a . - Without this line, we only know that we found line
c , which is the prefix b , and occurs in a , starting at position i , and the end of a not guaranteed to be reached.