Finding a common 3 character in array

I have an array eg:



I need to work out AAACCC by finding a common connection (if there is one)

In this case it’s: AAAZZZ ZZZCCC (ie the ZZZ is the connection)

So what’s the best way to find out that ZZZ is the 3 characters that are shared?

I would do something like this


str1 = ‘AAAZZZ’;
str2 = ‘ZZZCCC’;

  1. move pointer to the first char in str1

  2. then take a substring of that char plus the next 2 chars.

  3. search str2 for the existence of the substring in 2)

  4. move the pointer in 1) to the next char

  5. repeat steps 2-5 until a common string is found or you get to the 2nd last char in 1)