Android大法好！！

# leetCode 30.Substring with Concatenation of All Words (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 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 class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> list = new ArrayList<Integer>();
if(words.length == 0 || s.length() == 0){
return list;
}
Map<String,Integer> map = new HashMap<String,Integer>();//保存个数以及值
for(int i = 0; i < words.length; i++){
if(map.get(words[i]) == null){
map.put(words[i],1);//将word保存
}else{
map.put(words[i],map.get(words[i])+1);//将word保存的数值+1
}
}
Map<String,Integer> mapValue = new HashMap<String,Integer>(map);//保存重复的个数,方便重新赋值
int len = words.length;//去除重复之后的数组长度

int wordLen = words[0].length();
String temp = "";
int count = 0;//每个单词出现一次，就记录一次
for(int i = 0; i <= s.length() - len*wordLen;i++){
count = 0;//初始化
for(int j = 0; j < len;j++){
temp = s.substring(i + j * wordLen,i + (j+1) * wordLen);//截取wordLen长的字符串
if(map.get(temp) != null && map.get(temp) != 0){//如果map还有多于0个
map.put(temp,map.get(temp)-1);//map中数值减去1
count++;//记录数+1
}else{
break;
}
}
if(count == len){//如果记录数与len相等，则说明符合要求
}
//HashMap重新初始化
for(String key:map.keySet()){//这样更快速
map.put(key,mapValue.get(key));
}
}
return list;
}
}

#### LeetCode | Substring with Concatenation of All Words（链接所有单词的子串）

2014-08-17 20:54:32

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

2014-12-29 15:45:19

#### Substring with Concatenation of All Words -- LeetCode

2014-03-03 04:00:17

#### LeetCode（30） Substring with Concatenation of All Words

2015-08-24 20:51:05

#### LeetCode 30 Substring with Concatenation of All Words (C,C++,Java,Python)

2015-05-14 13:13:52

#### [Leetcode]30. Substring with Concatenation of All Words @python

2016-01-16 22:19:45

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

2015-03-06 22:14:12

#### Leetcode题：Substring with Concatenation of All Words

2014-03-26 21:23:50

#### (Java)LeetCode-30. Substring with Concatenation of All Words

2016-07-31 22:03:21

#### 【LeetCode】Substring with Concatenation of All Words

2014-04-30 15:29:28