解决 LeetCode 串联所有单词的子串问题

问题描述

给定一个字符串 s 和一个字符串数组 words words 中所有字符串 长度相同

 s 中的 串联子串 是指一个包含  words 中所有字符串以任意顺序排列连接起来的子串。

  • 例如,如果 words = ["ab","cd","ef"], 那么 "abcdef", "abefcd""cdabef", "cdefab""efabcd", 和 "efcdab" 都是串联子串。 "acdbef" 不是串联子串,因为他不是任何 words 排列的连接。

返回所有串联子串在 s 中的开始索引。你可以以 任意顺序 返回答案。

示例 1:

输入:s = "barfoothefoobarman", words = ["foo","bar"]
输出:[0,9]
解释:因为 words.length == 2 同时 words[i].length == 3,连接的子字符串的长度必须为 6。
子串 "barfoo" 开始位置是 0。它是 words 中以 ["bar","foo"] 顺序排列的连接。
子串 "foobar" 开始位置是 9。它是 words 中以 ["foo","bar"] 顺序排列的连接。
输出顺序无关紧要。返回 [9,0] 也是可以的。

示例 2:

输入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
输出:[]
解释:因为 words.length == 4 并且 words[i].length == 4,所以串联子串的长度必须为 16。
s 中没有子串长度为 16 并且等于 words 的任何顺序排列的连接。
所以我们返回一个空数组。

示例 3:

输入:s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
输出:[6,9,12]
解释:因为 words.length == 3 并且 words[i].length == 3,所以串联子串的长度必须为 9。
子串 "foobarthe" 开始位置是 6。它是 words 中以 ["foo","bar","the"] 顺序排列的连接。
子串 "barthefoo" 开始位置是 9。它是 words 中以 ["bar","the","foo"] 顺序排列的连接。
子串 "thefoobar" 开始位置是 12。它是 words 中以 ["the","foo","bar"] 顺序排列的连接。

提示:

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

解题思路

核心思想

我们需要找到 s 中所有包含 words 中所有单词的串联子串的起始索引。由于 words 中所有单词的长度相同,我们可以利用滑动窗口和哈希表来解决这个问题。

具体步骤

  1. 预处理

    • 计算每个单词的长度 wordLength 和所有单词的总长度 totalLength

    • 使用哈希表 wordCounts 记录 words 中每个单词的频率。

  2. 滑动窗口

    • 遍历 s 中所有可能的起始位置(从 0 到 s.length() - totalLength)。

    • 对于每个起始位置,检查从该位置开始的长度为 totalLength 的子串是否是一个有效的串联子串。

  3. 检查子串

    • 将子串分割成长度为 wordLength 的单词。

    • 使用另一个哈希表 currentCounts 记录当前子串中每个单词的频率。

    • 如果 currentCounts 与 wordCounts 完全匹配,则当前子串是一个有效的串联子串。

  4. 记录结果

    • 如果找到有效的串联子串,记录其起始索引。


代码实现

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> result = new ArrayList<>();
        if (s == null || words == null || words.length == 0) {
            return result;
        }

        int wordLength = words[0].length(); // 每个单词的长度
        int totalWords = words.length;     // 单词的总数
        int totalLength = wordLength * totalWords; // 串联子串的总长度

        // 统计 words 中每个单词的频率
        Map<String, Integer> wordCounts = new HashMap<>();
        for (String word : words) {
            wordCounts.put(word, wordCounts.getOrDefault(word, 0) + 1);
        }

        // 遍历所有可能的起始位置
        for (int i = 0; i <= s.length() - totalLength; i++) {
            // 当前窗口的单词频率
            Map<String, Integer> currentCounts = new HashMap<>();
            int j = 0;

            // 检查当前窗口是否匹配
            while (j < totalWords) {
                String word = s.substring(i + j * wordLength, i + (j + 1) * wordLength);
                if (!wordCounts.containsKey(word)) {
                    break; // 如果单词不在 words 中,直接跳出
                }
                currentCounts.put(word, currentCounts.getOrDefault(word, 0) + 1);

                // 如果当前单词的频率超过了 words 中的频率,跳出
                if (currentCounts.get(word) > wordCounts.get(word)) {
                    break;
                }
                j++;
            }

            // 如果完全匹配,将起始索引添加到结果中
            if (j == totalWords) {
                result.add(i);
            }
        }

        return result;
    }
}

复杂度分析

  • 时间复杂度O(n * m),其中 n 是字符串 s 的长度,m 是 words 的长度。

  • 空间复杂度O(m),用于存储 words 中单词的频率。


总结

通过滑动窗口和哈希表的方法,我们可以高效地解决这个问题。关键在于将问题分解为多个小步骤,并利用哈希表快速检查单词频率是否匹配。希望这篇博客能帮助你更好地理解这道题目的解法!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值