Leetcode 14. Longest Common Prefix [easy][java]

  1. Longest Common Prefix
    Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string “”.

Example
在这里插入图片描述

Solution 1
Time Complexity: O(S), where S is the sum of all characters in the string array. Space Complexity: O(1)

class Solution {
    public String longestCommonPrefix(String[] strs) {
        if(strs.length == 0)
            return "";
        if(strs.length == 1)
            return strs[0];
        
        String common = strs[0];
        
        
        for(int i = 1; i < strs.length; i++) {
            String temp = "";
            for(int j = 0; j < common.length() && j < strs[i].length(); j++) {
                if(common.charAt(j) == strs[i].charAt(j))
                    temp+=common.charAt(j);
                else
                    break;
            }
            common = temp;
        }
        return common;
    }
}

==Solution 2: horizontal search ==
Time Complexity: O(S), where S is the sum of all characters in all strings. Space Complexity: O(1)

class Solution {
    public String longestCommonPrefix(String[] strs) {
        if(strs.length == 0)
            return "";

        
        String prefix = strs[0];
        
        
        for(int i = 1; i < strs.length; i++) {
            while(strs[i].indexOf(prefix)!= 0) {
                prefix = prefix.substring(0, prefix.length()-1);
                if(prefix == "")
                    return prefix;
            }

        }
        return prefix;
    }
}

Solution 3: vertical search
Time Complexity: O(S), where S is the sum of all characters in all strings. In the worst case, there are n same string having m characters. So S = mn. In the best case, there are nminLen.
Space Complexity: O(1)

class Solution {
    public String longestCommonPrefix(String[] strs) {
        if(strs.length == 0)
            return "";
        
        for(int i = 0; i < strs[0].length(); i++) {
            char c = strs[0].charAt(i);
            for(int j = 1; j < strs.length; j++) {
                //check if the length of strs[j] is smaller than the prefix
                if(strs[j].length() == i || strs[j].charAt(i) != c)
                    return strs[0].substring(0,i);
            }
        }
        return strs[0];
    }
}

Solution 4: Divide and conquer
Time Complexity: O(S), where S is the sum of all characters in all strings.
Space Complexity: O(m⋅log(n)). As the length of the string array is n, we need to do log(n) recursions. Each iteration need m space to store the result.

class Solution {
    
    public String longestCommonPrefix(String[] strs) {
        if(strs.length == 0)
            return "";
        return longestCommonPrefix(strs, 0, strs.length-1);

    }
    
    public String longestCommonPrefix(String[] strs, int left, int right) {
        if(left == right)
            return strs[left];
        else {
            int mid = (left+right)/2;
            String lcpLeft = longestCommonPrefix(strs, left, mid);
            String lcpRight = longestCommonPrefix(strs, mid+1, right);
            return commonPrefix(lcpLeft, lcpRight);
        }
    }
    
    public String commonPrefix(String l, String r) {
        int min = Math.min(l.length(), r.length());
        for(int i = 0; i < min; i++) {
            if(l.charAt(i) != r.charAt(i))
                return l.substring(0, i);
        }
        
        return l.substring(0, min);
    }
}

Solution 5: Binary search
==Time Complexity: O(S *log(n)), where S is the sum of all character of all strings. Since the binary search will go through log(n) iterations and for each iteration there are S=m⋅n comparisons,
Space Complexity: O(1)

class Solution {
    
    public String longestCommonPrefix(String[] strs) {
        if(strs == null || strs.length == 0)
            return "";
        int min = strs[0].length();
        for(int i = 1; i < strs.length; i++) 
            min = Math.min(min, strs[i].length());
        
        int low = 1;
        int high = min;
        while(low <= high) {
            int middle = (low+high)/2;
            if(isCommonPrefix(strs, middle))
                low = middle + 1;
            else
                high = middle - 1;
                
        }
        return strs[0].substring(0, (low+high)/2);
    }
    
    private boolean isCommonPrefix(String[] strs, int len) {
        String temp = strs[0].substring(0, len);
        for(int i = 1; i < strs.length; i++) {
            if(!strs[i].startsWith(temp))
                return false;
        }
        return true;
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值