编程之法第一章习题——删除特定的字符

题目


给定一个原始字符串和模式字符串,要求在原始字符串中删除所有在模式字符串中出现过的字符,对应位置用空格占位。要求性能最优。例如:原始字符串为“They are students.”,模式字符串为“aeiou”,那么删除之后得到的字符串为“They r stdnts.”

代码


#pragma once
#include<iostream>
#include <map>
using namespace std;
class Solution
{
public:
    void deleteStr(string& rawStr, string& matchStr)
    {
        map<char, bool> matchTable;
        for (auto i:matchStr)
        {
            matchTable[i] = true;
        }
        int step = 0;
        for (auto iter = rawStr.begin(); iter != rawStr.end(); iter++)
        {
            *(iter - step) = *iter;
            if (matchTable[*iter])
            {
                step++;
            }

        }
        rawStr.erase(rawStr.end() - step, rawStr.end());
    }
};

猜你喜欢

转载自blog.csdn.net/m0_37316917/article/details/80209240