How to find and return all the common substrings of a string pair


I have googled a solution to find the longest common substring of a string pair. Now I want to find and return all the common substrings of a string pair. Does any one have a solution in Java handy?

By the way, I would like to have the longest common substring in the result set, just in case that the solution would remove it from the result set.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This is longest common subtsring from

public static int longestSubstr(String first, String second) {
    if (first == null || second == null || first.length() == 0 || second.length() == 0) {
        return 0;
    int maxLen = 0;
    int fl = first.length();
    int sl = second.length();
    int[][] table = new int[fl][sl];
    for (int i = 0; i < fl; i++) {
        for (int j = 0; j < sl; j++) {
            if (first.charAt(i) == second.charAt(j)) {
                if (i == 0 || j == 0) {
                    table[i][j] = 1;
                else {
                    table[i][j] = table[i - 1][j - 1] + 1;
                if (table[i][j] > maxLen) {
                    maxLen = table[i][j];
    return maxLen;

Open in new window

By the way there two more implemnattions in Java in the same linkk below
Are you talking about substrings or subsequences ?
I guess, substrings are consecutive
wsyyAuthor Commented:
I have founded the link, thanks.

I am talking about subsequences, with one condition that the length of each subsequence is more than 1.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.