LeetCode-1160: 拼写单词

First: Problem’s Description

Second: Problem’s Solution

Evidently we can use a hash vector to save the frequency of the letters in chars. then we traverse the vector wordsand each time we traverse a string in words, we copy Hash
as tmpHashto avoid change the raw Hash

Third: Code For Solution

class Solution {
   
public:
    int countCharacters(vector<string>& words, string chars) {
   
        vector<int> Hash(26, 0);
        for(int i = 0; i < chars.size(); i++)
            Hash[chars[i] - 'a']++;
        int len = 0;
        for(int i = 0; i < words.size(); i++){
   
            auto tmpHash = Hash;
            int tmplen = words[i].size();
            for(int j = 0; j < words[i].size(); j++){
   
                if(tmpHash[words[i][j] - 'a']-- > 0){
   }
                else{
   
                    tmplen = 0;
                    break;
                }  
            }
            len += tmplen;
        }
        return len;
    }
};

Four: Processing Results

全部评论

相关推荐

10-30 16:31
重庆大学 Java
代码飞升_不回私信人...:你说你善于学习,大家都会说。你说你是985,985会替你表达一切
点赞 评论 收藏
分享
LXXXXd:有点杂,想搞自动化的话没必要把法律的经历写上去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务