天池 在线编程 Character deletion

文章目录

1. 题目

描述
Enter two strings and delete all characters in the second string from the first string

字符串长度:[1, 10^5]

Example 1:
Input:  str=”They are students”,sub=”aeiou”
Output: ”Thy r stdnts”

来源:https://tianchi.aliyun.com/oj/141754208384739500/160296091929219251

2. 解题

  • 哈希查找
class Solution {
public:
    /**
     * @param str: The first string given
     * @param sub: The given second string
     * @return: Returns the deleted string
     */
    string CharacterDeletion(string &str, string &sub) {
        // write your code here
        unordered_set<char> s(sub.begin(), sub.end());
        string ans;
        for(char c : str)
        {
            if(!s.count(c))
                ans += c;
        }
        return ans;
    }
};

50ms C++


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页