首页 > 代码库 > LeetCode:Substring with Concatenation of All Words

LeetCode:Substring with Concatenation of All Words

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

For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]

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

解这道题开始的想法是S中依次截取和L中所有字符串长度相同的字符串。依次检查是否存在L[i]中的元素,如果L[i]中元素存在就删除这个字符串。那么最后如果字符串长度为0这个位置就是符合要求的位置。但是Time Limitted.

后来换一种解法,首先遍历一遍L数组,完成一个HashMap,map中key是L数组中元素,value是该字符串在数组中出现次数。

由于L中所有字符串长度相同,L中有num的字符串。一次循环,最多截取num个subString。截取一个先检测该subString是否存在于L中,不存在就跳出本次循环,开始下次循环。

最早忽略了L中可能存在相同的字符串的情况。


public class Solution {
    public List<Integer> findSubstring(String S, String[] L)
    {
        if(S.length() == 0 || L.length == 0)return new ArrayList<Integer>();
        int SLength = S.length();
        int LLength = L.length;
        int interval = L[0].length();
        ArrayList<Integer> indice = new ArrayList<Integer>();
        HashMap<String,Integer> map = new HashMap<String,Integer>();
        int i = 0;
        while(i<LLength)
        {
            if(map.containsKey(L[i]))
            {
                map.put(L[i], map.get(L[i])+1);
            }
            else
            {
                map.put(L[i], 1);
            }
            i++;
        }
        for(i=0;i<SLength-LLength*interval+1;i++)
        {
            HashMap<String,Integer> find = new HashMap<String,Integer>(map);
            int left = i;
            String sub = S.substring(left,left+interval);
            int num = LLength;
            while(contain(sub,L))
            {
                if(find.get(sub)==0)
                {
                    break;
                }
                else
                {
                    find.put(sub,find.get(sub)-1);
                    num = num - 1;
                    if(num == 0)
                    {
                        indice.add(i);
                        break;
                    }
                    left = left + interval;
                    sub = S.substring(left, left + interval);
                   
                }
            }
        }
       
        /*while(SLength - i>=interval)
        {
            String substring = S.substring(i, i + interval);
            for(int m = 0;m<LLength;m++)
            {
                if(substring.contains(L[m]))
                {
                    substring = delete(substring,L[m]);
                }
            }
            if(substring.isEmpty())
            {
                indice.add(i);
            }
            i++;
        }*/
       
        return indice;
    }
    public boolean contain(String str,String[] L)
    {
        int length = L.length;
        boolean b = false;
        for(int i = 0;i<length;i++)
        {
            if(str.equals(L[i]))
            {
                b = true;
                break;
            }
        }
        return b;
    }
}