LeetCode(30) Substring with Concatenation of All Words (java)

题目如下:

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.


For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]


You should return the indices: [0,9].
(order does not matter).


分析如下:

two-window型题目,两个指针,一块一慢。快的总共走了O(N),慢的也总共走了O(N),所以时间复杂度是2 * O(N),依然是O(N).



我的代码:

解法一,其实是O(N²)的,比较好理解。

public class Solution {
    
    public void initHash(HashMap<String, Integer> targetHash, String[] targetWords) {
        for (int i = 0; i < targetWords.length; ++i) {
            if (targetHash.containsKey(targetWords[i])) {
                targetHash.put(targetWords[i], targetHash.get(targetWords[i]) + 1);
            } else {
                targetHash.put(targetWords[i], 1);
            }
        }
    }
    
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> result = new ArrayList<Integer> ();
        if (words == null || words.length == 0 || s == null || s.length() == 0) {
            return result;
        }
        int totalWordCount = words.length, eachLength = words[0].length();
        HashMap<String, Integer> targetHash =  new HashMap<String, Integer> ();
        HashMap<String, Integer> sourceHash =  new HashMap<String, Integer> ();
        initHash(targetHash, words);
        for (int i = 0; i <= s.length() - eachLength * totalWordCount ; ++i) {
            sourceHash.clear();
            int j = 0;
            for (j = 0; j < totalWordCount; ++j) {
                int index = i + j * eachLength;
                String sub = s.substring(index, index + eachLength);
                if (!targetHash.containsKey(sub)) {
                    break;
                } 
                if (sourceHash.containsKey(sub)) {
                    sourceHash.put(sub, sourceHash.get(sub) + 1);
                } else {
                    sourceHash.put(sub, 1);
                }
                if (sourceHash.get(sub) > targetHash.get(sub)) {
                    break;
                }
            }
            if (j == totalWordCount) {
                result.add(i);
            }
        }
        return result;
    }
}



解法2: O(N)

摘抄自这里 CodeGanker blog http://blog.csdn.net/linhuanmars/article/details/20342851

public ArrayList<Integer> findSubstring(String S, String[] L) {
    // Note: The Solution object is instantiated only once and is reused by each test case.
    ArrayList<Integer> res = new ArrayList<Integer>();
    if(S==null || S.length()==0 || L==null || L.length==0)
        return res;
    HashMap<String,Integer> map = new HashMap<String,Integer>();
    for(int i=0;i<L.length;i++)
    {
        if(map.containsKey(L[i]))
        {
            map.put(L[i],map.get(L[i])+1);
        }
        else
        {
            map.put(L[i],1);
        }
    }
    for(int i=0;i<L[0].length();i++)
    {
        HashMap<String,Integer> curMap = new HashMap<String,Integer>();
        int count = 0;
        int left = i;
        for(int j=i;j<=S.length()-L[0].length();j+=L[0].length())
        {
            String str = S.substring(j,j+L[0].length());
            
            if(map.containsKey(str))
            {
                if(curMap.containsKey(str))
                    curMap.put(str,curMap.get(str)+1);
                else
                    curMap.put(str,1);
                if(curMap.get(str)<=map.get(str))
                    count++;
                else
                {
                    while(curMap.get(str)>map.get(str))
                    {
                        String temp = S.substring(left,left+L[0].length());
                        if(curMap.containsKey(temp))
                        {
                            curMap.put(temp,curMap.get(temp)-1);
                            if(curMap.get(temp)<map.get(temp))
                                count--;
                        }
                        left += L[0].length();
                    }
                }
                if(count == L.length)
                {
                    res.add(left);
                    //if(left<)
                    String temp = S.substring(left,left+L[0].length());
                    if(curMap.containsKey(temp))
                        curMap.put(temp,curMap.get(temp)-1);
                    count--;
                    left += L[0].length();
                }
            }
            else
            {
                curMap.clear();
                count = 0;
                left = j+L[0].length();
            }
        }
    }
    return res;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值