Java Array Compare longestCommonSubstring(String s[])

Here you can find the source of longestCommonSubstring(String s[])

Description

longest Common Substring

License

Open Source License

Declaration

static public String longestCommonSubstring(String s[]) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    static public String longestCommonSubstring(String s[]) {
        if (s.length == 0)
            return "";
        String res = s[0];/*from   ww w.j  ava  2  s .c o  m*/
        for (int i = 1; i < s.length; i++) {
            while (true) {
                if (s[i].startsWith(res))
                    break;
                if (res.length() == 0)
                    return "";
                res = res.substring(0, res.length() - 1);
            }
        }
        return res;
    }

    static public String longestCommonSubstring(Vector v) {
        if (v.size() == 0)
            return "";
        String res = (String) v.elementAt(0);
        for (int i = 1; i < v.size(); i++) {
            while (true) {
                String s = (String) v.elementAt(i);
                if (s.startsWith(res))
                    break;
                if (res.length() == 0)
                    return "";
                res = res.substring(0, res.length() - 1);
            }
        }
        return res;
    }
}

Related

  1. getLongestCommonSubsequence(int[] a, int[] b)
  2. getRelativeSegments(String[] targetPath, int commonSegments, int discardedSegments)
  3. longestCommonPrefix(String[] stringArray)
  4. longestCommonSubsequence(E[] s1, E[] s2)
  5. longestCommonSubsequenceAlternate(int[] input)