Having 2 strings like:
string a = "ATTAGACCTGCCGGAA";
string b = "GCCGGAATAC";
I would like to just delete the part that is common in both strings and then the rest concatenate it. I have to tell that what I need to delete only left matched part so I would get
input
ATTAGACCTGCCGGAA
GCCGGAATAC
output
ATTAGACCTGCCGGAATAC
Firstly I thought to use a pattern and then seacrh for it, however this is not possible as I do not know the pattern in advance (the length of matched chars is variable)
Then I thought on search whole string b
in a
then if had no succes, delete a char in string a
(Last one since I want to preserve most left unmatched string) and then loop until I have no more chars in b
like
string a = "ATTAGACCTGCCGGAA";
string b = "GCCGGAATAC";
int times = b.Length;
string wantedString = string.Empty;
string auxString = b;
while (times > 0)
{
if (!a.Contains(auxString))
{
//save last char and then delete it from auxString
wantedString += auxString[auxString.Length - 1];
auxString = auxString.TrimEnd(auxString[auxString.Length - 1]);
}
else
break;
times--;
}
//reverse string
char[] reversedToAppend = wantedString.ToCharArray();
Array.Reverse(reversedToAppend);
string toAppend = new string(reversedToAppend);
so the answer would be just to do a + toAppend ;
Is there a way to make this more efficient? (maybe in LINQ?)
Edit
As @lavin points out correctly c
can occur anywhere in a
, while being a prefix of b. for instance if a=AAT
and b=AAG
, code should return AATG
.
the reason is because common string starting on left is c=AA
. We delete this from b
and then we get a=AAT
with the resulting G
AAT
AAG
resulting
AATG
Other example would be:
a=ATTTGGGCCGCGCGCGAAAACCCCGCG
b= AACCCCGCGCGCA
here
c= AACCCCGCG
so result should be
result = ATTTGGGCCGCGCGCGAAAACCCCGCGCGCA