稀碎从零算法笔记Day55-LeetCode:100291. 统计特殊字母的数量 II

今天可惜了,周赛第二题没看出来,导致第三题时间都不够,最后一题...

题目描述:

给你一个字符串 word。如果 word 中同时出现某个字母 c 的小写形式和大写形式,并且 每个 小写形式的 c 都出现在第一个大写形式的 c 之前,则称字母 c 是一个 特殊字母 。

返回 word 中 特殊字母 的数量。

C++代码

class Solution {
public:
    int numberOfSpecialChars(string word) {
        int ans = 0;
        unordered_map<char,int>hash;
        for(int i=0;i<word.length();++i){
            if(word[i] > 'Z'&& hash.find(word[i])!=hash.end())
                hash[word[i]] = i;
            else
                hash.insert({word[i],i});
        }
        for(auto inte : hash){
            //能找到大写的话
            if(hash.find(toupper(inte.first))!=hash.end()){
                char temp =toupper(inte.first);
                if(inte.second < hash[temp])
                    ++ans;
            }
        }
        return ans;
        
    }
};

相关推荐

  1. 算法笔记Day40-LeetCode:加油站

    2024-04-22 10:14:07       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-22 10:14:07       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-22 10:14:07       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-22 10:14:07       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-22 10:14:07       20 阅读

热门阅读

  1. Leetcode 4.21

    2024-04-22 10:14:07       11 阅读
  2. 算法之前缀和和差分

    2024-04-22 10:14:07       12 阅读
  3. 广州大学2023-2024学年第一学期《计算机网络》A卷

    2024-04-22 10:14:07       16 阅读
  4. SWCTF

    SWCTF

    2024-04-22 10:14:07      17 阅读
  5. 负载均衡原理及算法

    2024-04-22 10:14:07       12 阅读
  6. Sentinel

    Sentinel

    2024-04-22 10:14:07      12 阅读
  7. 阿里云难题学习笔记

    2024-04-22 10:14:07       11 阅读