Java Array Common Prefix longestCommonSubstring(String S1, String S2)

Here you can find the source of longestCommonSubstring(String S1, String S2)

Description

longest Common Substring

License

Apache License

Declaration

public static String longestCommonSubstring(String S1, String S2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String longestCommonSubstring(String S1, String S2) {
        int Start = 0;
        int Max = 0;
        for (int i = 0; i < S1.length(); i++) {
            for (int j = 0; j < S2.length(); j++) {
                int x = 0;
                while (S1.charAt(i + x) == S2.charAt(j + x)) {
                    x++;/*from  w  ww  .j a va2s .c  o  m*/
                    if (((i + x) >= S1.length()) || ((j + x) >= S2.length()))
                        break;
                }
                if (x > Max) {
                    Max = x;
                    Start = i;
                }
            }
        }
        return S1.substring(Start, (Start + Max));
    }
}

Related

  1. longestCommonPrefix3(String[] strs)
  2. longestCommonPrefix4(String[] strs)
  3. longestCommonSequence(String str1, String str2)
  4. longestCommonSubstr(String s1, String s2)
  5. LongestCommonSubString(String firstString, String secondString)
  6. longestCommonSuffix(final byte[] seq1, final byte[] seq2, final int maxLength)