今天可惜了,周赛第二题没看出来,导致第三题时间都不够,最后一题...
题目描述:
给你一个字符串 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;
}
};