码迷,mamicode.com
首页 > 其他好文 > 详细

longest common prefix

时间:2015-06-24 00:35:28      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

1. Question

找字符串数组的最长公共前缀,是所有字符串的。

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

2. Solution(O(mn))

以第一个字符串作为前缀初值(前缀不可能比它要长,可以优化采用长度最短的字符串为初值字符串),遍历所有字符串,求当前字符串与前缀变量的公共前缀,作为新的前缀变量。

考虑特殊情况:数组为空或数组长度为0

技术分享
import java.util.Arrays;
import java.util.LinkedList;

public class Solution {
    //O(mn)
    public String longestCommonPrefix(String[] strs){
        if( strs.length == 0 )
            return "";
        
        int len = strs[0].length();
        int k = 0;
        for( int i=1; i<strs.length; i++ )
            if( strs[i].length() < len ){
                len = strs[i].length();
                k = i;
            }
        String prefix = strs[k];
        for( int i=0; i<strs.length; i++){
            if( !strs[i].startsWith( prefix ) ){
                prefix = prefix.substring(0, prefix.length()-1 );
                i--;
            }
        }
        return prefix;
    }
}
View Code

或者一个一个比较:

技术分享
import java.util.Arrays;
import java.util.LinkedList;

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

        int k = 0;
        for( int i=1; i<strs.length; i++ )
            if( strs[i].length() < strs[k].length() )
                k = i;
        
        int len = strs[k].length();
        int i;
        for( i=0; i<len; i++ ){
            boolean judge = true;
            for( int j=0; judge && j<strs.length; j++ )
                judge = judge && ( strs[j].charAt(i) == strs[k].charAt(i) );
            if( judge == false )
                break;
        }
        return strs[k].substring(0, i);
    }
}
View Code

也可以用递归,即最长公共前缀是前n-1个字符串的最长公共前缀与最后一个字符的最长公共前缀:

技术分享
import java.util.Arrays;
import java.util.LinkedList;

public class Solution {
    //use iteration, O(n2)
    public String longestCommonPrefix(String[] strs){
        if( strs.length == 0 )
            return "";
        if( strs.length == 1 )
            return strs[0];
        return longestCommonPrefix( new LinkedList<String>( Arrays.asList( strs ) ) );        
    }
    
    public String longestCommonPrefix( LinkedList<String> strs ){
        if( strs.size() == 2){
            String s1 = strs.getFirst();
            String s2 = strs.getLast();
            int len = (s1.length() < s2.length()) ? s1.length() : s2.length();
            int i = 0;
            for( ; i<len && s1.charAt(i) == s2.charAt(i); i++);
            return s1.substring(0, i);
        }
        
        LinkedList<String> temp = new LinkedList<String>();
        temp.add( strs.removeLast());
        temp.add( longestCommonPrefix(strs) );
        return longestCommonPrefix( temp );
    }
}
View Code

 

longest common prefix

标签:

原文地址:http://www.cnblogs.com/hf-cherish/p/4596517.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!