【LeetCode】Substring with Concatenation of All Words 解题报告

【题目】

You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once
and without any intervening characters.

For example, given:

S: "barfoothefoobarman"

L: ["foo", "bar"]

You should return the indices: [0,9].

(order does not matter).

【解析】

题意:给定一个字符串S和一个字符串数组L,L中的字符串长度都相等,找出S中所有的子串恰好包含L中所有字符各一次,返回子串的起始位置。

把L转化为一个HashMap<String, Integer>,其value表示L中String的个数,因为L中可能包含相同的字符串。

public class Solution {
    public List<Integer> findSubstring(String S, String[] L) {
        List<Integer> ans = new ArrayList<Integer>();
        if (S.length() < 1 || L.length < 1) return ans;
        int len = L[0].length(); //题目说L中每个单词长度一样

        //初始化HashMap,注意L中可能包含多个相同的字符串,所以用value表示个数
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        for (int j = 0; j < L.length; j++) {
            if (map.containsKey(L[j])) {
            	map.put(L[j], map.get(L[j]) + 1);
            } else {
            	map.put(L[j], 1);
            }
        }

        //i的范围很关键,如果直接到S.length()是会超时的
        for (int i = 0; i <= S.length() - L.length * len; i++) {
            int from = i;
            String str = S.substring(from, from + len);
            int cnt = 0;
            while (map.containsKey(str) && map.get(str) > 0) {
                map.put(str, map.get(str) - 1);
                cnt++;
                from += len;
                if (from + len > S.length()) break; //注意越界
                str = S.substring(from, from + len);
            }

            //L中每个单词恰好出现一次,加入到结果集
            if (cnt == L.length) {
                ans.add(i);
            }

            //为下一次初始化HashMap
            if (cnt > 0) {
            	map.clear();
                for (int j = 0; j < L.length; j++) {
                    if (map.containsKey(L[j])) {
                    	map.put(L[j], map.get(L[j]) + 1);
                    } else {
                    	map.put(L[j], 1);
                    }
                }
            }
        }

        return ans;
    }
}

这道题的Test Case有点变态,代码改了好多次,各种没有注意到的边边角角。

时间: 2024-10-10 02:47:39

【LeetCode】Substring with Concatenation of All Words 解题报告的相关文章

LeetCode: Substring with Concatenation of All Words 解题报告

Substring with Concatenation of All WordsYou are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any interve

Substring with Concatenation of All Words——解题报告(窗口移动法)

[题目] You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and without any intervening characters. For example,

LeetCode:Substring with Concatenation of All Words (summarize)

题目链接 You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters. For example, given:

LeetCode: Substring with Concatenation of All Words [029]

[题目] You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters. For example, given:

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

[LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and without any intervening characters. For example, give

[LeetCode] Substring with Concatenation of All Words

Substring with Concatenation of All Words You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and without any

LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the arr

[LeetCode] Substring with Concatenation of All Words 串联所有单词的子串

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. For example, giv