[leetcod]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).

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

思路1:假设L中的单位长度为n,依次从S中取长度为n的子串,如果在L中,就记下来。需要借助hash或map,如果整个L都匹配完了,就算是一个concatenation;当匹配错误的时候,S右移一个位置。

思路2(参考2):优化,双指针法,思想类似 Longest Substring Without Repeating Characters, 复杂度可以达到线性。

思路1:

public class Solution {
	public ArrayList<Integer> findSubstring(String S, String[] L) {
		if (S == null || L == null)
			return null;
		int size = L.length;
		int len = L[0].length();
		ArrayList<Integer> res = new ArrayList<Integer>();

		HashMap<String, Integer> expected = new HashMap<String, Integer>();
		for (String each : L) {
			Integer old = expected.get(each);
			if (old == null)
				expected.put(each, 1);
			else
				expected.put(each, old + 1);
		}
		HashMap<String, Integer> real = new HashMap<String, Integer>();
		int i;

		for (i = 0; i <= S.length() - size * len; i++) {
			real.clear();

			int j, k = 0;
			for (j = i; j < i + size * len; j = j + len, k++) {
				String sub = S.substring(j, j + len);
				if (expected.containsKey(sub)) {
					Integer old = real.get(sub);
					if (old == null)
						real.put(sub, 1);
					else
						real.put(sub, old + 1);

					if (real.get(sub) > expected.get(sub))
						break;

				} else
					break;

			}
			if (k == size)
				res.add(i);

		}

		return res;
	}

	public static void main(String[] args) {
		// String S = "barfoothefoobarman";
		// String[] L = { "foo", "foo" };
		// System.out.println(new Solution().findSubstring(S, L));

		String S = "a";
		String[] L = { "a" };
		System.out.println(new Solution().findSubstring(S, L));
	}
}

思路2(待实现中):

参考:

http://blog.csdn.net/ojshilu/article/details/22212703

http://blog.csdn.net/linhuanmars/article/details/20342851

[leetcod]Substring with Concatenation of All Words

时间: 2024-11-08 04:51:32

[leetcod]Substring with Concatenation of All Words的相关文章

[LeetCode] 030. Substring with Concatenation of All Words (Hard) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 030. Substring with Concatenation of All Words (Hard) 链接: 题目:https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ 代码(github):https://gi

[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】Substring with Concatenation of All Words

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 interv

[Leetcode][Python]30: Substring with Concatenation of All Words

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 30: Substring with Concatenation of All Wordshttps://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ You are given a string, S, and a list of words, L, that are all of the

【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: "b

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:

每日算法之二十六:Substring with Concatenation of All Words

变相的字符串匹配 给定一个字符串,然后再给定一组相同长度的单词列表,要求在字符串中查找满足以下条件的起始位置: 1)从这个位置开始包含单词列表中所有的单词,且每个单词仅且必须出现一次. 2)在出现的过程中不能出现其他的干扰单词. 3)出现的位置可能有多个. 4)单词的出现顺序不做要求. 下面是一个例子: S:"barfoothefoobarman" L:"foo","bar" 位置0是出现位置,:两个单词均出现仅出现一次,且没有干扰.同样位置9也

LeetCode HashTable 30 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

[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