Substring with Concatenation of All Words

問題描述

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).

思考:問題類似與Longest Substring Without Repeating Characters 和Minimum Window Substring

想法

  • 1、維護一個dictmap,保存words的單詞以及個數,map保存s的字串(已經遍歷的並且帶驗證的)。設total爲單詞總數。
  • 2、維護兩個index:left(start index)和j(當前index),設置一個count保存當前已加入map的單詞數。
  • 3、從j出去字串(長度爲len)str,如果dict中包含str,則將str加入map,並且count++,此時,如果map中str的value大於dict中str的value,則需要將left後移(即left 到j+len , 這段序列包含太多單詞,需要刪除),這個過程需要涉及count的變化。
  • 4、如果count == total,則表示map與dict,重的值相同,即找到一個start index,在結果鏈表中加入left。
  • 5、將left + len,繼續循環;

代碼:

public class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        int n = s.length();
        int total = words.length;
        List<Integer> ret =  new LinkedList<>();
        if (n <= 0 || total <= 0) return ret;
        Map<String,Integer> dict = new HashMap<>();
        for(String word:words){
            Integer value = dict.get(word);
            if(value == null)
                dict.put(word,1);
            else
                dict.put(word,value + 1);
        }
        int len = words[0].length();
        for(int i = 0; i < len; i++){
            Map<String,Integer> subStr = new HashMap<>();
            int left = i;
            int count = 0;
            String start = null;
            for(int j = i; j <= n - len; j += len){
                String temp = s.substring(j,j + len);
                Integer num = dict.get(temp);
                if(num != null){
                    Integer value = subStr.get(temp);
                    if (value == null)
                        value = 1;
                    else value += 1;
                    subStr.put(temp,value);

                    if(value <= num)
                        count++;
                    else
                        while(subStr.get(temp) > dict.get(temp)){
                            String str = s.substring(left, left + len);
                            Integer valueStr = subStr.get(str);
                            if(--valueStr < dict.get(str))  count--;
                            subStr.put(str,valueStr);
                            left += len;
                        }

                    if(count == total){
                        ret.add(left);
                        count--;
                        String str = s.substring(left,left + len);
                        subStr.put(str,subStr.get(str) - 1);
                        left += len;
                    }

                }
                else {
                    left = j + len;
                    count = 0;
                    subStr.clear();
                }
            }
        }
        return ret;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章