1160. spell words

2020-03-17

1. Title Description

拼写单词

2. Parse

直接进行搜索即可

3. Code

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    int countCharacters(vector<string>& words, string chars) {
         int l = words.size(),ll,i,j,k,cnt=0;
         bool f[110];
         for (i=0;i<l;i++){
            memset(f,false,sizeof(f));
            string tmp=words[i];
            ll=tmp.length();
            for (j=0;j<ll;j++){
                for (k=0;k<chars.length();k++){
                    if (tmp[j]==chars[k]){
                        if (!f[k]) {
                            f[k]=true;
                            break;
                        }
                    }
                }
                if (k>=chars.length()) break;
            }
            if (j>=ll) cnt+=ll;
         }
        return cnt;
    }
};

int main(){
    Solution s;
    vector<string> words={"cat","bt","hat","tree"};
    string chars="atach";
    cout<<s.countCharacters(words,chars)<<endl;
    return 0;
}
Published 179 original articles · won praise 79 · views 70000 +

Guess you like

Origin blog.csdn.net/qq_34600424/article/details/104918539