30. 串联所有单词的子串-字符串、hashmap-困难

问题描述

给定一个字符串 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"]
输出:[]

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

题解

//滑动窗口,每次维护一个长度为words所有单词总长的s子串,在每个子串中search words中的每个单词。运行速度超过40%的人
class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        if(s==null || words.length==0)return new ArrayList<Integer>();
        List<Integer> result = new ArrayList<Integer>();//保存结果
        int i, j, value, len_s = s.length(), len_words = 0, len_words_each = words[0].length(), flag;
        String sub1, sub2;
        for(String temp:words)len_words+=temp.length();
        if(len_words > len_s)return result;
        Map<String,Integer> map2 = new HashMap<String,Integer>(), map;//重置map
        for(String temp:words){
            if(map2.containsKey(temp))map2.put(temp,map2.get(temp)+1);
            else map2.put(temp,1);
        }
        for(i=0;i<len_s;i++){
            flag = 1;
            if(i+len_words <= len_s){
                map =  new HashMap<String,Integer>(map2);//重置map
                sub1 = s.substring(i,i+len_words);
                for(j=0;j<len_words;j++){
                    if(j+len_words_each <= len_words){
                        sub2 = sub1.substring(j,j+len_words_each);
                        if((value = map.getOrDefault(sub2,-1)) > 0){
                            map.put(sub2,value-1);
                            j += len_words_each-1;
                        }else{
                            flag = 0;
                            break;
                        }
                    }
                }
            }else break;
            if(flag == 1)result.add(i);
        }
        return result;
    }
}
/* 超时的全排列。。。。。。。。。。。。。。。。。。。
class Solution {
    public void dfs(List<String> pailie, int first, int len, List<String> res){
        if(first == len){
            StringBuilder s = new StringBuilder();
            for(String temp:pailie)
                s.append(temp);
            if(!res.contains(s.toString()))res.add(s.toString());
        }
        for(int i=first;i<len;i++){
            Collections.swap(pailie, i, first);
            dfs(pailie, first+1, len, res);
            Collections.swap(pailie, i, first);
        }
    }
    public List<Integer> findSubstring(String s, String[] words) {
        if(s==null || words.length==0)return new ArrayList<Integer>();
        List<Integer> result = new ArrayList<Integer>();//保存结果
        List<String> res = new ArrayList<String>();//保存全排列结果
        List<String> pailie = new ArrayList<String>(Arrays.asList(words));//将String[]类型的words转为list,便于得到全排列
        int len_words = words.length, len = s.length(), len_res, i;
        dfs(pailie, 0, len_words, res);
        len_res = res.get(0).length();
        for(i=0;i<len;i++)
            if(i+len_res <= len && res.contains(s.substring(i,i+len_res)))result.add(i);
        return result;
    }
}
*/
原文地址:https://www.cnblogs.com/xxxxxiaochuan/p/13355935.html