30. 串联所有单词的子串

给定一个字符串 s 和一些 长度相同 的单词 words 。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。

注意子串要与 words 中的单词完全匹配,中间不能有其他字符 ,但不需要考虑 words 中单词串联的顺序。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

方法一

class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> res = new ArrayList<>();
        if (s == null || s.length() == 0 || words == null || words.length == 0) return res;
        HashMap<String, Integer> map = new HashMap<>();
        int one_word = words[0].length();
        int word_num = words.length;
        int all_len = one_word * word_num;
        for (String word : words) {
            map.put(word, map.getOrDefault(word, 0) + 1);
        }
        for (int i = 0; i < s.length() - all_len + 1; i++) {
            String tmp = s.substring(i, i + all_len);
            HashMap<String, Integer> tmp_map = new HashMap<>();
            for (int j = 0; j < all_len; j += one_word) {
                String w = tmp.substring(j, j + one_word);
                tmp_map.put(w, tmp_map.getOrDefault(w, 0) + 1);
            }
            if (map.equals(tmp_map)) res.add(i);
        }
        return res;
    }
}

方法二

import java.util.*;

class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        if (s == null || s.length() == 0 || words == null || words.length == 0) {
            return Collections.emptyList();
        }
        int oneLength = words[0].length();
        int allNum = words.length;
        int allLength = oneLength * allNum;
        Map<String, Integer> allMap = new HashMap<>();
        for (String word : words) {
            Integer num = allMap.getOrDefault(word, 0);
            allMap.put(word, num + 1);
        }
        List<Integer> ret = new ArrayList<>();
        for (int i = 0; i < oneLength; ++i) {
            Map<String, Integer> slideWindow = new HashMap<>();
            int matchCount = 0;
            int left = i, right = i;
            while (right + oneLength <= s.length()) {
                String word = s.substring(right, right + oneLength);
                right += oneLength;
                if (!allMap.containsKey(word)) {
                    left = right;
                    matchCount = 0;
                    slideWindow = new HashMap<>();
                } else {
                    slideWindow.put(word, slideWindow.getOrDefault(word, 0) + 1);
                    ++matchCount;
                    while (slideWindow.get(word) > allMap.get(word)) {
                        String w = s.substring(left, left + oneLength);
                        slideWindow.put(w, slideWindow.get(w) - 1);
                        left += oneLength;
                        --matchCount;
                    }
                }
                if (matchCount == allNum) {
                    ret.add(left);
                }
            }
        }
        return ret;
    }
}

posted @ 2021-12-22 21:39  Tianyiya  阅读(31)  评论(0)    收藏  举报