LeetCode30. 串联所有单词的子串

30. 串联所有单词的子串 题目链接

解法一思路:

暴力匹配,每次取 s 定长字符串,与 words 单词匹配,若能完全匹配则将子串起始索引加入结果

匹配规则:当前遍历字符串(s.substring(i, i + wordLen))在 words 存在(去除完全不可能的匹配),且与 words 组成的哈希表也一致。

public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> result = new ArrayList<>();
        if (s == null || s.length() < 1 || words == null || words.length < 1) {
            return result;
        }
        int wordLen = words[0].length(), totalLen = wordLen * words.length;
        Map<String, Integer> wordsMap = new HashMap<>();
        for (String word : words) {
            wordsMap.put(word, wordsMap.getOrDefault(word, 0) + 1);
        }

        for (int i = 0; i <= s.length() - totalLen; i++) {
            if (wordsMap.containsKey(s.substring(i, i + wordLen)) &&
                    isEqual(s.substring(i, i + totalLen), wordsMap, wordLen)) {
                result.add(i);
            }
        }
        return result;
    }

    private boolean isEqual(String subStr, Map<String, Integer> wordsMap, int wordLen) {
        Map<String, Integer> subStrMap = new HashMap<>();
        for (int i = 0; i < subStr.length(); i += wordLen) {
            String tempStr = subStr.substring(i, i + wordLen);
            subStrMap.put(tempStr, subStrMap.getOrDefault(tempStr, 0) + 1);
        }
        return subStrMap.equals(wordsMap);
    }

解法二:滑动窗口,优化字符串匹配

public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> result = new ArrayList<>();
        if (s == null || s.length() < 1 || words == null || words.length < 1) {
            return result;
        }
        int wordLen = words[0].length(), totalLen = wordLen * words.length;
        Map<String, Integer> wordsMap = new HashMap<>();
        for (String word : words) {
            wordsMap.put(word, wordsMap.getOrDefault(word, 0) + 1);
        }

        for (int i = 0; i < wordLen; i++) {
            int left = i, right = i, match = 0;
            Map<String, Integer> window = new HashMap<>();
            while (right <= s.length() - wordLen) {
                String tempStr = s.substring(right, right + wordLen);
                if (wordsMap.containsKey(tempStr)) {
                    window.put(tempStr, window.getOrDefault(tempStr, 0) + 1);
                    if (window.get(tempStr) == wordsMap.get(tempStr)) {
                        match++;
                    }
                }
                right += wordLen;
                while (wordsMap.size() == match) {
                    if (right - left == totalLen) {
                        result.add(left);
                    }
                    String temp = s.substring(left, left + wordLen);
                    if (wordsMap.containsKey(temp)) {
                        window.put(temp, window.get(temp) - 1);
                        if (window.get(temp) < wordsMap.get(temp)) {
                            match--;
                        }
                    }
                    left += wordLen;
                }
            }
        }
        return result;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值