Sometimes I feel this site just gives wrong answers to confuse you..Below runs just fine locally.


  • 0
    A
    #include <stdio.h>
    #include <stdlib.h>
    
    /*
    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).
    */
    
    
    /**
     * Return an array of size *returnSize.
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int* findSubstring(char* s, char** words, int wordsSize, int* returnSize)
    {
      int final_size = 1000;
      int* final = (int*) malloc (sizeof(int)* final_size);
      int* word_array_index = (int)malloc(sizeof(int) * wordsSize);
      char* word = NULL;
      int j = 0;
      int k = 0;
      int prev_index = -1;
      int final_index = 0;
      int found_word = 0;
      int last_stored_index = -1;
      int m = 0;
      *returnSize =0;
    
      if(s == NULL || words[0] == NULL || wordsSize == 0)
      return NULL;
    
      char* input_str = s;
      int i =0;
      memset(word_array_index,-1,sizeof(int) * wordsSize);
    
      while(s[i] != '\0' && (strlen(s)-i) >= wordsSize)
      {
          j=0;
    
    
          while(words[j] != NULL)
          {
              word = words[j];
              k=0;
              m=0;
              if(s[i+k] == word[k])
              {
                  while(k < wordsSize && s[i+k]==word[k]){k++;}
                  while(word_array_index[m]!= -1 && word_array_index[m]!= j) {m++; }
                  if(k==wordsSize && word_array_index[m]== -1)
                  {
                      word_array_index[m]=j;
                      if(prev_index == -1)
                      {prev_index = i;}
                      else{
                         if((prev_index + i+ k )% wordsSize == 0)
                         {
                             if(prev_index != last_stored_index )
                             {
                                  final[final_index++]= prev_index;
                                  (*returnSize)++;
                                  last_stored_index = prev_index;
                             }
    
                         }
                      }
                      i = i+wordsSize -1;
                      found_word = 1;
                      break;
                  }
                  else if(k==wordsSize && word_array_index[m]!= -1)
                  { memset(word_array_index,-1,sizeof(int) * wordsSize); i--;break;}
              }
    
              j++;
          }
          if(found_word == 0)
          {
            prev_index = -1;
          }
          found_word = 0;
          i++;
      }
    
      return final;
    
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.