Problem:
You is given a string, s, and a list of words, words, that is all of the same length. Find all starting indices of substring (s) in s that's 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).
Solution: Because the length is fixed, fixed the position where each string starts, then searches backwards to see if each string exists, if it does not exist i++
Java source Code (658MS):
public class Solution {public list<integer> findsubstring (String s, string[] words) {Map<string,integ Er> tmp,map=new hashmap<string,integer> (); int Lens=s.length (), lenw=words[0].length (); for (int i=0;i<words.length;i++) {if (Map.containskey (words[i))) {Map.put (Words[i],map.get (Word S[i]) +1); }else{Map.put (words[i],1); }} list<integer> res = new arraylist<integer> (); for (int i=0;i<=lens-lenw*words.length;i++) {tmp=new hashmap<string,integer> (); int j=0; for (; j<words.length;j++) {int pos=i+j*lenw; String sub=s.substring (POS,POS+LENW); if (Map.containskey (sub)) {int num=0; if (Tmp.containskey (sub)) Num=tmp.get (sub); if (Map.get (sub) < num+1) break; else Tmp.put (sub,num+1); }else break; } if (j>=words.length) {res.add (i); }} return res; }}
C Language Source code (260MS): Sub Not Free will be MLE
typedef struct node{char* Word; int times; struct node* Next;} data; #define SIZE 1000int 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<len;i++ ) {Sub[i]=s[i+start];} Sub[i]=0;return Sub;} int* findsubstring (char* s, char** words, int wordssize, int* returnsize) {int Lenw=strlen (words[0]), Lens=strlen (s), le Ngth=wordssize; int* res= (int*) malloc (sizeof (int) * (lens-lenw*length+1)); data** map= (data**) malloc (sizeof (data*) *size); data** tmp= (data**) malloc (sizeof (data*) *size); int I,j;for (i=0;i<size;i++) {map[i]=null;tmp[i]=null;} for (i=0;i<length;i++) {insertmap (MAP,WORDS[I],LENW); }*returnsize=0;for (I=0;i<lens-lenw*length+1;i++) {for (j=0;j<size;j++) {if (tmp[j]!=null) {free (tmp[j]); tmp[j]=null;}} for (j=0;j<length;j++) {char* sub=substring (s,i+j*lenw,lenw), int mapnum=findmap (map,sub), if (mapnum==-1) break; int Num=insertmap (TMP,SUB,LENW), if (Mapnum < num) Break;free (sub);} if (j>=length) res[(*returnsize) ++]=i;} for (i=0;i<size;i++) if (map[i]!=null) free (map[i]), free (map); return res;}
C + + source code (704MS):
Class Solution {public: vector<int> findsubstring (string s, vector<string>& words) { int lens= S.size (), Lenw=words[0].size (), length=words.size (); Map<string,int> strmap,tmp; for (int i=0;i<length;i++) { strmap[words[i]]++; } vector<int> Res; for (int i=0;i<lens-lenw*length+1;i++) { tmp.clear (); int j=0; for (j=0;j<length;j++) { string sub=s.substr (I+J*LENW,LENW); if (Strmap.find (sub) = = Strmap.end ()) break; tmp[sub]++; if (Strmap[sub] < tmp[sub]) break; } if (j>=length) res.push_back (i); } return res; }};
Python source code (706MS):
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
Leetcode Substring with concatenation of all Words (C,c++,java,python)