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, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]
You should return the indices: [0,9]
.
(order does not matter).
public class Solution {
public List findSubstring(String s, String[] words) {
Map tmp,map=new HashMap();
int lens=s.length(),lenw=words[0].length();
for(int i=0;i res = new ArrayList();
for(int i=0;i<=lens-lenw*words.length;i++){
tmp=new HashMap();
int j=0;
for(;j=words.length){
res.add(i);
}
}
return res;
}
}
typedef struct Node{
char* word;
int times;
struct Node* next;
}data;
#define SIZE 1000
int hash(char* word){
int i=0,h=0;
for(;word[i];i++){
h=(h*31+word[i])%SIZE;
}
return h;
}
int InsertMap(data** map,char* word,int lenw){
int h = hash(word);
if(map[h]==NULL){
map[h]=(data*)malloc(sizeof(data));
map[h]->word=(char*)malloc(sizeof(char)*(lenw+1));
map[h]->times=1;
strcpy(map[h]->word,word);
map[h]->next=NULL;
return 1;
}else{
data* p=map[h];
while(p->next!=NULL){
if(strcmp(p->word,word)==0){
p->times++;
return p->times;
}
p=p->next;
}
if(strcmp(p->word,word)==0){
p->times++;
return p->times;
}else{
data* tmp=(data*)malloc(sizeof(data));
tmp->word=(char*)malloc(sizeof(char)*(lenw+1));
tmp->times=1;
strcpy(tmp->word,word);
tmp->next=NULL;
p->next=tmp;
return 1;
}
}
}
int FindMap(data** map,char* sub){
int h = hash(sub);
if(map[h]==NULL){
return -1;
}else{
data* p=map[h];
while(p!=NULL){
if(strcmp(p->word,sub)==0){
return p->times;
}
p=p->next;
}
return -1;
}
}
char* substring(char* s,int start,int len){
char* sub=(char*)malloc(sizeof(char)*(len+1));
int i=0;
for(;i=length)res[(*returnSize)++]=i;
}
for(i=0;i
class Solution {
public:
vector findSubstring(string s, vector& words) {
int lens=s.size(),lenw=words[0].size(),length=words.size();
map strmap,tmp;
for(int i=0;i res;
for(int i=0;i=length)res.push_back(i);
}
return res;
}
};
class Solution:
# @param {string} s
# @param {string[]} words
# @return {integer[]}
def findSubstring(self, s, words):
lens=len(s);lenw=len(words[0]);length=len(words)
map={};res=[]
for i in range(length):
if words[i] in map:map[words[i]]+=1
else:map[words[i]]=1
if not words[i] in s:return res
for i in range(lens-lenw*length+1):
tmp={};j=0;flag=True
for j in range(length):
pos=i+j*lenw
sub=s[pos:pos+lenw]
if sub in map:
num=0
if sub in tmp:num=tmp[sub]
if map[sub] < num+1:flag=False;break
tmp[sub]=num+1
else:flag=False;break
if flag:res.append(i)
return res