Why my solution got MLE(Merroy Limit Exceeded)


  • 0
    A

    I think my solution maybe TLE but it got a MLE. Can anyone tell me why? Thanks,

    My solution:

    1. Firstly, compute all the concatenations of all the words

    2. Then, find in S for each concatenation and record their index

      class Solution {
      public:
      std::vectorstd::string connect(std::string word, std::vectorstd::string middleResult, int len){
      std::vectorstd::string result;
      int resultLen = middleResult.size();
      for(int i = 0; i < resultLen; ++i){
      int wordLen = middleResult[i].size();
      for(int j = 0; j <= wordLen; j += len){
      result.push_back(middleResult[i].substr(0, j) + word + middleResult[i].substr(j));
      }
      }
      return result;
      }
      std::vectorstd::string concatenation(std::vectorstd::string &words){
      int len = words.size();
      std::vectorstd::string result;
      if(len == 0){
      return result;
      }
      if(len == 1){
      result.push_back(words[0]);
      return result;
      }
      int wordLen = words[0].size();
      if(wordLen == 0){
      result.push_back("");
      return result;
      }
      std::vectorstd::string newWords(words.begin() + 1, words.end());
      std::vectorstd::string middleResult = concatenation(newWords);
      result = connect(words[0], middleResult, wordLen);
      return result;
      }

      std::vector<int> findSubstring(std::string S, std::vector<std::string> &L) {
      	std::vector<int> result;
      	std::vector<std::string> wordStr = concatenation(L);
      	int wordStrLen = wordStr.size();
      	if(wordStrLen == 0){
      		return result;
      	}
      	int strLen = wordStr[0].size();
      	int len = S.size();
      	if(strLen > len){
      		return result;
      	}
      	for(int i = 0; i < wordStrLen; ++i){
      		size_t start = 0;
      		while((start = S.find(wordStr[i], start)) != std::string::npos){
      			result.push_back(start);
      			start += wordStr[i].size(); 
      		}
      	}
      	return result;
       }
      

      };


  • 0
    R

    Please leave some comments between your code lines.


  • 0
    P

    Ayumi, are you trying to put all word permutations into one vector? It's a huge number.


Log in to reply
 

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