天题系列:Substring with Concatenation of All Words

滑动窗口,但是很繁琐

 public class Solution {
    public ArrayList<Integer> findSubstring(String S, String[] L) {
        //http://www.cnblogs.com/springfor/p/3872516.html
        ArrayList<Integer> res = new ArrayList<Integer>();
        if(S==null || L==null || S.length()==0 || L.length==0 ||L[0].length()==0)  return res;
        HashMap<String, Integer> dict = new HashMap<String, Integer>();
        int wl = L[0].length();

        for(String i: L){
            if(dict.containsKey(i)){
                dict.put(i, dict.get(i)+1);
            }else
                dict.put(i, 1);
        }

        for(int i=0; i<wl; i++){
            int ind = i;  // window‘s starting index
            int cnt = 0;                        // check how many words included in the window, and it should not be more than L‘s length
            HashMap<String, Integer> curdic = new HashMap<String, Integer>();
          // for(int j = 0; j<S.length()-wl; j+=wl){ // check all words in S
              for(int j = i; j<=S.length()-wl; j+=wl){
                String curwd = S.substring(j, j+wl);
                if(!dict.containsKey(curwd)){ // means there is no such word in L, clear curdic and move on(Concatenation must be continuous),
                    curdic.clear();
                    cnt=0;
                    ind = j+wl;
                }else{
                    if(!curdic.containsKey(curwd)){
                        curdic.put(curwd, 1);
                    }else{
                        curdic.put(curwd,curdic.get(curwd)+1);
                    }

                // is the window curdic overflow? now check cnt
                    if(curdic.get(curwd)<=dict.get(curwd)){
                        cnt++;
                    }else{
                         while(curdic.get(curwd)>dict.get(curwd)){ // slide the window and remove the words(tmp) till curwd
                             String tmp = S.substring(ind, ind+wl);
                                curdic.put(tmp, curdic.get(tmp)-1);
                                ind += wl;
                                if(curdic.get(tmp)<dict.get(tmp))
                                    cnt--;
                        }
                    }
                        // is cnt already == L? which means the window has all L‘s words
                    if(cnt==L.length){
                         res.add(ind);
                         String tmp = S.substring(ind, ind+wl);
                         curdic.put(tmp, curdic.get(tmp)-1);
                         ind += wl;
                         cnt--;   // 注意这里cnt的挪动,因为同一个i起始点,window curdic可以挪,cnt是决定是否记录一下这个位置
                    }
                }
            }
        }
        return res;
    }
}
 
时间: 2024-10-05 22:19:41

天题系列: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: "b

LeetCode:Substring with Concatenation of All Words (summarize)

题目链接 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:

每日算法之二十六:Substring with Concatenation of All Words

变相的字符串匹配 给定一个字符串,然后再给定一组相同长度的单词列表,要求在字符串中查找满足以下条件的起始位置: 1)从这个位置开始包含单词列表中所有的单词,且每个单词仅且必须出现一次. 2)在出现的过程中不能出现其他的干扰单词. 3)出现的位置可能有多个. 4)单词的出现顺序不做要求. 下面是一个例子: S:"barfoothefoobarman" L:"foo","bar" 位置0是出现位置,:两个单词均出现仅出现一次,且没有干扰.同样位置9也

leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法

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 an

leetCode 30.Substring with Concatenation of All Words (words中所有子串相连) 解题思路和方法

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 an

[LeetCode] 030. Substring with Concatenation of All Words (Hard) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 030. Substring with Concatenation of All Words (Hard) 链接: 题目:https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ 代码(github):https://gi

[LeetCode] Substring with Concatenation of All Words

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 wordsexactly once and without any

【leetcode】Substring with Concatenation of All Words

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 interv

[Leetcode][Python]30: Substring with Concatenation of All Words

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 30: Substring with Concatenation of All Wordshttps://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ You are given a string, S, and a list of words, L, that are all of the

微软100题系列之-----设计包含min函数的栈

题意: 定义栈的数据结构,要求添加一个min 函数,能够得到栈的最小元素. 要求函数min.push 以及pop 的时间复杂度都是O(1). 思路:定义两个栈,一个用来记录数据的插入和删除,一个用来存储最小值的变化 代码如下: template <class T> class Stack { public: Stack(int len=100); T Min(); T Pop(); void Push(T val); private: T top1,top2; T *stack1,*stack