1. 程式人生 > >刪除模式串中出現的字符

刪除模式串中出現的字符

時間復雜度 address res pos 模式串 || ddr 刪除 hash

刪除模式串中出現的字符,如“welcome to asted”,模式串為“aeiou”那麽得到的是“wlcm t std”。


#include<iostream>
#include<cstring>

using namespace std;

char *re(char *str,char *model)
{
   if(str==NULL||model==NULL)
     return NULL;
    
   int Hash[26]={0};
   
   for(int i=0;i<strlen(model);i++)
   {
       
       ++Hash[model[i]-'a'];        
   } 
   char *address=str;
   int index=0;
   for(int i=0;i<strlen(str);i++)
   {
      
       if(str[i]==' '|| Hash[str[i]-'a']==0 )
       {
         str[index]=str[i];
         index++;
       }
       
   }
    str[index]='\0';
    return address;
   //
}

int main()
{
   char str[]="welcome to asted";
   char model[]="aeiou";

   cout<<re(str,model)<<endl;
}
時間復雜度為O(n),空間復雜度為O(1)。

刪除模式串中出現的字符