30.Substring with Concatenation of All Words

原创 2016年08月30日 17:14:50

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, given:
s"barfoothefoobarman"
words["foo", "bar"]

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

(order does not matter).

public List<integer> findSubstring(String s, String[] words) {
	int N = s.length();
	List<integer> indexes = new ArrayList<integer>(s.length());
	if (words.length == 0) {
		return indexes;
	}
	int M = words[0].length();
	if (N < M * words.length) {
		return indexes;
	}
	int last = N - M + 1;
	// 很简单的数据处理
	
	//map each string in words array to some index and compute target counters
	Map<string integer=""> mapping = new HashMap<string integer="">(words.length);
	int [][] table = new int[2][words.length];
	int failures = 0, index = 0;
	for (int i = 0; i < words.length; ++i) {
		Integer mapped = mapping.get(words[i]);
		if (mapped == null) {
			++failures;
			mapping.put(words[i], index);
			mapped = index++;
		}
		++table[0][mapped];
	}
	// 将题目中的数据放到目标容器中
	
	//find all occurrences at string S and map them to their current integer, -1 means no such string is in words array
	int [] smapping = new int[last];
	for (int i = 0; i < last; ++i) {
		String section = s.substring(i, i + M);
		Integer mapped = mapping.get(section);
		if (mapped == null) {
			smapping[i] = -1;
		} else {
			smapping[i] = mapped;
		}
	}
	// 到这里题目就变成对数组的处理了,真的不得不说原作者的思路真的很巧
	
	//fix the number of linear scans
	for (int i = 0; i < M; ++i) {
		//reset scan variables
		int currentFailures = failures; //number of current mismatches
		int left = i, right = i;
		Arrays.fill(table[1], 0);
		//here, simple solve the minimum-window-substring problem
		while (right < last) {
			while (currentFailures > 0 && right < last) {
				int target = smapping[right];
				if (target != -1 && ++table[1][target] == table[0][target]) {
					--currentFailures;
				}
				right += M;
			}
			while (currentFailures == 0 && left < right) {
				int target = smapping[left];
				if (target != -1 && --table[1][target] == table[0][target] - 1) {
					int length = right - left;
					//instead of checking every window, we know exactly the length we want
					if ((length / M) ==  words.length) {
						indexes.add(left);
					}
					++currentFailures;
				}
				left += M;
			}
		}
		
	}
	return indexes;
}
// 如果对过程不理解的,可以去实际测试数据并调试或中间输出。
// 耽误时间好久~</string></string></integer></integer></integer>
版权声明:本文为博主原创文章,转载须注明出处,谢谢~

[理解leetcode解法]30. Substring with Concatenation of All Words找子串

[理解leetcode解法]30. Substring with Concatenation of All Words找子串

LeetCode(30)--Substring with Concatenation of All Words

转载自博客 LeetCode30———–Substring with Concatenation of All Words 看题目都有些费劲,要不是看了几组Custom Testcase才知道题意...

【C++】 LeetCode 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. Fin...
  • lv1224
  • lv1224
  • 2017年08月01日 15:31
  • 109

Leetcode 30 Substring with Concatenation of All Words 无序map的应用细节

先说说unordered_map。在做这题之前我是不知道这个数据结构的,我只会用map,这题学到了。 两者的接口等基本一致,写代码会map的话,应该也会unordered_map。 map的实现是红黑...

Leetcode #30. Substring with Concatenation of All Words 连续子串查找 解题报告

1 解题思想题目说给了一个原串,然后给了一组单词,让你从这个原串当中找一个区间,这个区间正好包含了这些所有的单词,不能交叉,不能有多余的值这道题是我在Leetcode 刷的最艰辛的一道题,我可能不适合...
  • MebiuW
  • MebiuW
  • 2016年04月01日 23:40
  • 348

【leetcode】String——Substring with Concatenation of All Words(30)

题目: You are given a string, s, and a list of words, words, that are all of the same length. Fin...

30. Substring with Concatenation of All Words

此道题目,我一开始用的解法是暴力法,通过两个map来判断符合条件的子串。时间复杂度为O(NM),其中N为字符串s的长度,M为words数组中字符串的个数。需要注意的一点是,在实际的运用中,unorde...

leetcode 30 -- Substring with Concatenation of All Words

Substring with Concatenation of All Words 题目: You are given a string, s, and a list of words, ...

LeetCode_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 sta...

30. Substring with Concatenation of All Words (又超时了,最后一个case没过)

You are given a string, s, and a list of words, words, that are all of the same length. Find all sta...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:30.Substring with Concatenation of All Words
举报原因:
原因补充:

(最多只允许输入30个字)