leetcode Substring with Concatenation of All Words

本文探讨了在解决字符串子串查找问题时遇到的问题,并通过使用map数据结构优化了算法,提高了效率。主要内容包括错误代码分析、解决方案实现及性能提升。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚开始做此题时考虑的不完善,没有对L中出现重复的元素进行考虑,

有误的代码

class Solution {
public:
   
    vector<int> findSubstring(string S, vector<string> &L) 
    {
        vector<int> res;
        
        int len = S.length();
        int numOfWords = L.size();
        if(len==0||numOfWords==0)
            return res;
        vector<string> flag;
        int lenOfWord = L[0].length();
        if(lenOfWord*numOfWords > len)
            return res;
        int tempSize = 0;
        int indice = INT_MAX;
        
        for(int i = 0; i < numOfWords; ++i)
		{
			if(!isFindSubstr(L[i], flag))
			{
				flag.push_back(L[i]);		
			}
		
		}
		numOfWords = flag.size();
        
        
        
        int i;
        for(i = 0; i < len; ++i)
        {
            if(isFindSubstr(S.substr(i,lenOfWord), L)&&tempSize==0)
            {
                 //判断所有连续的
                 while(i < len)
                 {
                    string substr = S.substr(i,lenOfWord);
                    if(isFindSubstr(substr, L)&&!isFindSubstr(substr, flag))
                    {
                        indice = min(indice, i);
                        flag.push_back(substr);
                        tempSize ++;
                        i += lenOfWord;
                    }
                    else
                    {
						--i;
						break;
                      
                    }
                 }

				 if(tempSize==numOfWords)
                 {
                     res.push_back(indice);                          
                  }
				  flag.clear();
                  indice = INT_MAX; 
                  tempSize = 0;
                  
            }
           
        }
        
        return res;
        
    }
    
     bool isFindSubstr(string str, vector<string> &L)
    {
        for(int i = 0; i < L.size(); ++i)
        {
            if(str==L[i])
                return true;
        }
        
        return false;
    }
};



利用map后Accepted的代码

class Solution {
public:
   
    vector<int> findSubstring(string S, vector<string> &L) 
    {
        vector<int> res;
        
        int len = S.length();
        int numOfWords = L.size();
        if(len==0||numOfWords==0)
            return res;

        int lenOfWord = L[0].length();
        if(lenOfWord*numOfWords > len)
            return res;
        
        map<string, int> flag;
        for(int i = 0; i < numOfWords; ++i)
		{
			flag[L[i]]++;
		}
		
		 int indice = 0;
		 map<string, int> temp;
		 while(indice <= (len - lenOfWord*numOfWords))
		 {
		     bool flagTmp = true;
		     temp.clear();
		     for(int i = indice; i <= indice + lenOfWord*(numOfWords-1); i += lenOfWord)
		     {
		         string tmp = S.substr(i,lenOfWord);
		         if(flag.find(tmp) == flag.end())
		         {
		             flagTmp = false;
		             break;
		         }
		         temp[tmp]++;
		         if(temp[tmp]>flag[tmp])
		         {
		             flagTmp = false;
		             break;
		         }
		         
		     }
		     
		     if(flagTmp==true)
		        res.push_back(indice);
		        
		     indice++;
		 }
		 
		 return res;
        
    }
     
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值