[LeetCode 30] Substring with Concatenation of All Words (Hard)

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.

Example 1:

Input:
  s = "barfoothefoobarman",
  words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.

Example 2:

Input:
  s = "wordgoodgoodgoodbestword",
  words = ["word","good","best","word"]
Output: []

Solution: HashMap

参考https://www.cnblogs.com/grandyang/p/4521224.html

  1. 要求从S中,找出所有包含全部words里面String连在一起的substring。(只能是words里面的词连在一起,不能出现其他的词。而且,words中的词,但如果出现的次数比words中的次数还多,也不允许。 比如,"goodgoodword", ["good", "word"], 那么"goodgoodword"就不行, 但是"goodword"就可以)。

  2. 假设 words 数组中有n个单词,每个单词的长度均为 len,那么就是找出所有长度为 n*len 的子串,使得其刚好是由 words 数组中的所有单词组成。

    • 每次都需要判断s串中长度为 len 的子串是否是 words 中的单词,
    • 用一个baseTrack的HashMap来记录words中词及其频次。
    • 用HashMap,用一个baseMap记录words中所有词和频次。用另一个map每一次判断时,用来判断当前词是否在baseMap中,同时出现的次数不会比words中的次数还多。
  3. 遍历s中所有长度为 nlen 的子串,当剩余子串的长度小于 nlen 时,就不用再判断了。所以从0开始,到 (int)s.size() - n*len 结束。

  4. 对于每个遍历到的长度为 n*len 的子串,需要验证其是否刚好由 words 中所有的单词构成. 检查方法:

    • 就是每次取长度为 len 的子串,看其是否是 words 中的单词, 不要直接从array中检查是否包含,如果是很大的array时,会超时。直接检查在baseMap中是否包含这个词即可。
    • 一旦取出的词不在 words 中,直接 break ;否则在new HashMap 中的映射值加1。
    • 同时检测若其映射值超过原 HashMap 中的映射值,也 break ,因为就算当前单词在 words 中,但若其出现的次数超过 words 中的次数,还是不合题意的。
    • 在 for 循环外面,若count正好等于n,说明我们检测的n个长度为 len 的子串都是 words 中的单词,并且刚好构成了 words,则将当前位置startIndex加入结果即可。
class Solution {
    public List findSubstring(String s, String[] words) {
        List result = new ArrayList<> ();
        
        if ((s == null || s.length () == 0) || (words == null || words.length == 0))
            return result;
        
        if (words != null && words.length != 0 && s.length () < words[0].length () * words.length) {
            return result;
        }
        
        //1. base hashmap
        Map baseTracker = new HashMap<> ();
        for (int i = 0; i < words.length; i++) {
            int frequency = baseTracker.getOrDefault (words[i], 0) + 1;
            baseTracker.put (words[i], frequency);
        }
        
        //2. try to find all matched substring, increase the start index one by one 
        int wordNumber = words.length;
        int wordLen = words[0].length ();
        Map tracker = new HashMap<> ();
        
        for (int start = 0; start <= s.length () - wordNumber * wordLen; start ++) {
            // whenever start to find all matches, need to clear or new a hashmap
            tracker = new HashMap<> ();
            
            // how much words matches
            int count = 0;
            for (count = 0; count < wordNumber; count ++) {
                int newStartIndex = start + count * wordLen;
                String substr = s.substring (newStartIndex, newStartIndex + wordLen);
                
                if (!baseTracker.containsKey (substr)) {
                    break;
                }
                
                int frequency = tracker.getOrDefault (substr, 0) + 1;
                tracker.put (substr, frequency);
                
                if (frequency > baseTracker.getOrDefault (substr, 0)) {
                    break;
                }  
            }
            
            // find one all matches words, and push the index
            if (count == wordNumber) {
                result.add (start);
            }
        }
        
        return result;
    }
}

你可能感兴趣的:([LeetCode 30] Substring with Concatenation of All Words (Hard))