Java Array Common Prefix longestCommonPrefix(String[] strs, int l, int r)

Here you can find the source of longestCommonPrefix(String[] strs, int l, int r)

Description

longest Common Prefix

License

Open Source License

Declaration

private static String longestCommonPrefix(String[] strs, int l, int r) 

Method Source Code

//package com.java2s;

public class Main {
    private static String longestCommonPrefix(String[] strs, int l, int r) {
        if (l == r) {
            return strs[l];
        } else {/*  www. ja  v  a  2  s.com*/
            int mid = (l + r) / 2;
            String lcpLeft = longestCommonPrefix(strs, l, mid);
            String rcpRight = longestCommonPrefix(strs, mid + 1, r);
            return commonPrefix(lcpLeft, rcpRight);
        }
    }

    private static String commonPrefix(String left, String right) {
        int min = Math.min(left.length(), right.length());
        for (int i = 0; i < min; i++) {
            if (left.charAt(i) != right.charAt(i)) {
                return left.substring(0, i);
            }
        }
        return left.substring(0, min);
    }
}

Related

  1. longestCommonPrefix(String[] strs)
  2. longestCommonPrefix(String[] strs)
  3. longestCommonPrefix(String[] strs)
  4. longestCommonPrefix(String[] strs)
  5. longestCommonPrefix(String[] strs)
  6. longestCommonPrefix1(String[] strs)
  7. longestCommonPrefix2(String[] strs)
  8. longestCommonPrefix3(String[] strs)
  9. longestCommonPrefix4(String[] strs)