【LeetCode】串联所有单词的子串 java

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

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

示例 1:

输入:s = "barfoothefoobarman", words = ["foo","bar"]
输出:[0,9]
解释:
从索引 0 和 9 开始的子串分别是 "barfoo" 和 "foobar" 。
输出的顺序不重要, [9,0] 也是有效答案。
示例 2:

输入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
输出:[]
示例 3:

输入:s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
输出:[6,9,12]

提示:

1 <= s.length <= 104
s 由小写英文字母组成
1 <= words.length <= 5000
1 <= words[i].length <= 30
words[i] 由小写英文字母组成

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words

public List findSubstring(String s, String[] words) {
        List values = new ArrayList<>(); // 返回结果
        int wordNum = words.length; // 单词数量
        int wordLenth = words[0].length(); // 单词长度
        if (wordLenth == 0) {
            return values;
        }
        int wordAlllen = wordNum * wordLenth; // 单词组成串的长度
        Map allWords = new HashMap<>(); // 存放所有的单词
        // 遍历所有单词
        for (String word : words) {
            allWords.put(word, allWords.getOrDefault(word,0)+1);
        }

        // 遍历字符串s
        for (int i = 0; i < s.length() - wordAlllen +1; i++) {

            HashMap hasWord = new HashMap<>();// 存放截取字符串包含在单词map的情况
            int num = 0; //遍历的单词数量
            // 遍历的单词数量 少于 单词数字 总数量就一直执行 
            while (num < wordNum) {
                // 截取单个单词长度的子串 
                String substring = s.substring(i+num*wordLenth, i +(num+1) *wordLenth);
                 // 如果截取的子串在 allWords 中,就把该子串“单词” 存在 hasWord 
                if (allWords.containsKey(substring)) {
                    hasWord.put(substring, hasWord.getOrDefault(substring,0)+1);
                    // 如果存在 hasWord 中的单词数量 超过对应单词总数量,说明该 substring  不正确
                    if (hasWord.get(substring) > allWords.get(substring)) {
                        break;
                    }
                } else {
                    break;
                }
                num ++;
            }

            if (num == wordNum) {
                values.add(i);
            }

        }
        return values;
    }

你可能感兴趣的:(LeetCode刷题,leetcode,算法,哈希表)