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

全部评论

相关推荐

11-07 16:07
深圳大学 运营
前端飞升:学长,阿里不是卡双非吗,我深也能去吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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