算法-滑动窗口-串联所有单词的子串

算法-滑动窗口-串联所有单词的子串

1 题目概述

1.1 题目出处

https://leetcode.cn/problems/substring-with-concatenation-of-all-words/

1.2 题目描述


2 滑动窗口+Hash表

2.1 解题思路

  1. 构建一个大小为串联子串的总长的滑动窗口
  2. 为每个words中的子串创建一个hash表, <子串值,子串出现次数>
  3. 记录每次开始位置,从左往右遍历字符串s,每次截取words[0]长度的子串,和2中hash表对比,如果没有或者使用次数超限,就表示该组合无法构成,退出该次检测;否则继续检测,直到构成的串联子串长度满足要求或者已检测长度超限。构建成功时,记录下起始序号
  4. 一轮检测完成后,窗口向右滑动1个长度,继续下一轮检测

2.2 代码

java 复制代码
class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> resultList = new ArrayList<>();
        int windowSize = words[0].length();
        if (windowSize > s.length()) {
            return resultList;
        }
        int strLength = windowSize * words.length;
        if (strLength > s.length()){
            return resultList;
        }
        Map<String, Integer> wordMap = new HashMap<>();
        for (int i = 0; i < words.length; i++) {
            Integer subKeyTimes = wordMap.get(words[i]);
            if (null == subKeyTimes) {
                subKeyTimes = 0;
            } 
            wordMap.put(words[i], subKeyTimes + 1);
        }

        for (int i = 0; i <= s.length() - strLength; i++) {
            int result = getStart(s, words, strLength, windowSize, i, wordMap);
            if (result != -1) {
                resultList.add(result);
            }
        }
        return resultList;
    }

    private int getStart(String s, String[] words, int strLength, int windowSize, int first, Map<String, Integer> wordMap) {
        int start = -1;
        int length = 0;
        Map<String, Integer> useMap = new HashMap();
        for (int i = first; i < s.length() && length < strLength && i < first + strLength; i += windowSize) {
            String sub = s.substring(i, i + windowSize);
            Integer subKeyTimes = wordMap.get(sub);
            if (null == subKeyTimes) {
                return -1;
            }
            Integer useTimes = useMap.get(sub);
            if (null == useTimes) {
                useTimes = 1; 
            } else {
                useTimes += 1;
            }
            if (useTimes > subKeyTimes) {
                return -1;
            }
            useMap.put(sub, useTimes);
            length += windowSize;
            if (start == -1) {
                start = i;
            }
        }
        if (length == strLength) {
            return start;
        }
        return -1;
    }
}

2.3 时间复杂度

s.length=N,words.length=M ,时间复杂度O(N*M)

2.4 空间复杂度

O(M)

3 双滑动窗口+Hash表

3.1 解题思路

使用两个滑动窗口:

  1. 外层滑动窗口,每次移动大小为1,最多移动words[0]长度
  2. 内层滑动窗口,每次移动大小为words[0]

相当于把words[0]长度倍数的子串放在一起,用内层滑动窗口进行解析:

  1. 如果遇到当前子串不在目标字符串s,则舍弃之前记录,从下一个子串开始
  2. 如果遇到当前子串在目标字符串s内:
    1. 使用次数未超限,则记录使用次数、首个拼接子串下标、并累加拼接长度,再看是否已经拼接目标串联子串成功:
      • 如果成功则记录下标并移除首个子串,继续检测下一个子串;
      • 如果未成功,直接继续继续检测下一个子串;
    2. 使用次数超限,则不断移除已拼接子串内首个子串,直到次数不超限为止,并继续检测下一个子串

3.2 代码

java 复制代码
class Solution {
     public List<Integer> findSubstring(String s, String[] words) {
            List<Integer> resultList = new ArrayList<>();
            int windowSize = words[0].length();
            if (windowSize > s.length()) {
                return resultList;
            }
            int strLength = windowSize * words.length;
            if (strLength > s.length()){
                return resultList;
            }
            Map<String, Integer> wordMap = new HashMap<>();
            for (int i = 0; i < words.length; i++) {
                Integer subKeyTimes = wordMap.get(words[i]);
                if (null == subKeyTimes) {
                    subKeyTimes = 0;
                }
                wordMap.put(words[i], subKeyTimes + 1);
            }

            for (int i = 0; i < windowSize; i += 1) {
                List<Integer> subResultList = getStart(s, strLength, windowSize, i, wordMap);
                if (subResultList.size() > 0) {
                    resultList.addAll(subResultList);
                }
            }
            return resultList;
        }

        private List<Integer> getStart(String s, int strLength, int windowSize, int first, Map<String, Integer> wordMap) {
            List<Integer> resultList = new ArrayList();
            int start = -1;
            int length = 0;
            Map<String, Integer> useMap = new HashMap();

            for (int i = first; i <= s.length() - windowSize; i += windowSize) {
                String sub = s.substring(i, i + windowSize);
                Integer subKeyTimes = wordMap.get(sub);
                if (null == subKeyTimes) {
                    // 子串找不到,清空useMap
                    useMap.clear();
                    start = -1;
                    length = 0;
                    continue;
                }
                length += windowSize;
                Integer useTimes = useMap.get(sub);
                if (null == useTimes) {
                    useTimes = 1;
                } else {
                    useTimes += 1;
                    useMap.put(sub, useTimes);
                    while (useTimes > subKeyTimes) {
                        // 子串使用次数超出存在的子串条数,删除第一个子串计数,直到不超出为止
                        String firstSub = s.substring(start, start + windowSize);
                        useMap.put(firstSub, useMap.get(firstSub) - 1);
                        useTimes = useMap.get(sub);
                        start = start + windowSize;
                        length -= windowSize;
                    }
                }
                useMap.put(sub, useTimes);
                if (start == -1) {
                    start = i;
                }
                if (length == strLength) {
                    // 成功凑成一个串联子串
                    // 记录开始下标
                    resultList.add(start);
                    // 移除首个子串
                    String firstSub = s.substring(start, start + windowSize);
                    useMap.put(firstSub, useMap.get(firstSub) - 1);
                    start = start + windowSize;
                    length -= windowSize;
                }
            }

            return resultList;
        }
}

3.3 时间复杂度

s.length=N,words.length=M ,时间复杂度O(N)

3.4 空间复杂度

O(M)

参考文档

相关推荐
MATLAB代码顾问7 分钟前
Python实现蜂群算法优化TSP问题
开发语言·python·算法
代码飞天11 分钟前
机器学习算法和函数整理——助力快速查阅
人工智能·算法·机器学习
jiushiapwojdap19 分钟前
LU分解法求解线性方程组Matlab实现
数据结构·其他·算法·matlab
笨笨饿33 分钟前
69_如何给自己手搓一个串口
linux·c语言·网络·单片机·嵌入式硬件·算法·个人开发
纽扣6671 小时前
【算法进阶之路】链表进阶:删除、合并、回文与排序全解析
数据结构·算法·链表
消失的旧时光-19431 小时前
统一并发模型:线程、Reactor、协程本质是一件事(从线程到协程 · 第6篇·终章)
java·python·算法
智者知已应修善业2 小时前
【51单片机不用数组动态数码管显示字符和LED流水灯】2023-10-3
c++·经验分享·笔记·算法·51单片机
AI进化营-智能译站2 小时前
ROS2 C++开发系列16-智能指针管理传感器句柄|告别ROS2节点内存泄漏与野指针
java·c++·算法·ai
CS创新实验室3 小时前
从盘边到芯端——硬盘接口七十年变迁史
算法·磁盘调度
xvhao20133 小时前
单源、多源最短路
数据结构·c++·算法·深度优先·动态规划·图论·图搜索算法