longest Substr - Java java.lang

Java examples for java.lang:String Substring

Description

longest Substr

Demo Code


//package com.java2s;

public class Main {
    public static int longestSubstr(String first, String second) {
        if (first == null || second == null || first.length() == 0
                || second.length() == 0) {
            return 0;
        }//from  w w  w .  ja  v  a  2  s  . c o  m

        int maxLen = 0;
        int fl = first.length();
        int sl = second.length();
        int[][] table = new int[fl + 1][sl + 1];

        for (int s = 0; s <= sl; s++)
            table[0][s] = 0;
        for (int f = 0; f <= fl; f++)
            table[f][0] = 0;

        for (int i = 1; i <= fl; i++) {
            for (int j = 1; j <= sl; j++) {
                if (first.charAt(i - 1) == second.charAt(j - 1)) {
                    if (i == 1 || j == 1) {
                        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;
    }
}

Related Tutorials