LeetCode 30 串联所有单词的子串

Posted Starzkg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 30 串联所有单词的子串相关的知识,希望对你有一定的参考价值。

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;
        int one_word = words[0].length();
        int word_num = words.length;
        HashMap<String, Integer> map = new HashMap<>(word_num);
        for (String word : words) 
            map.put(word, map.getOrDefault(word, 0) + 1);
        
        HashMap<String, Integer> tmp_map = new HashMap<>(word_num);
        for (int i = 0; i < one_word; i++,tmp_map.clear()) 
            for (int j = i + one_word ; j <= s.length() ; j+=one_word) 
                String word = s.substring(j -one_word, j);
                tmp_map.put(word, tmp_map.getOrDefault(word, 0) + 1);
                if (j/one_word > word_num - 1)
                    int e0 = j - word_num * one_word;
                    String s0= s.substring(e0 ,e0 + one_word);
                    if (map.equals(tmp_map)) res.add(e0);
                    tmp_map.put(s0,tmp_map.get(s0)-1);
                    tmp_map.remove(s0,0);
                
            
        
        return res;
    


以上是关于LeetCode 30 串联所有单词的子串的主要内容,如果未能解决你的问题,请参考以下文章

算法leetcode|30. 串联所有单词的子串(rust重拳出击)

LeetCode 30 串联所有单词的子串

LeetCode 30 串联所有单词的子串

Leetcode 30. 串联所有单词的子串

Python描述 LeetCode 30. 串联所有单词的子串

leetcode——30. 串联所有单词的子串