Iterate over each letter in str1 , checking its existence in str2 . If it does not exist, go to the next letter, if this happens, increase the length of the substring in str1 , which you check in str2 , by two characters and repeat until further matches are found, or you iterate through str1 .
This will find all the substrings that are common, but similar to bubbles - are hardly optimal, but a very simple example of how to solve the problem.
Something like this pseudo-descending example:
pos = 0 len = 1 matches = []; while (pos < str1.length()) { while (str2.indexOf(str1.substring(pos, len))) { len++; } matches.push(str1.substring(pos, len - 1)); pos++; len = 1; }
source share