llllmz

导航

383. 赎金信C

\

 int hash(char c){
    return c-'a';
}

bool canConstruct(char* ransomNote, char* magazine) {
    if(!ransomNote) return true;
    if(!magazine) return false;
    int a[26]={0};
    int b[26]={0};
    int i=0,j=0;
    while(ransomNote[i]!=0){
        a[hash(ransomNote[i++])]++;
    }
    while(magazine[j]!=0){
        b[hash(magazine[j++])]++;
    }
    if(i>j) return false;
    for(int x=0;x<26;x++){
        if(a[x]>b[x]) return false;
    }
    return true;
}

结果:

posted on 2024-02-28 19:58  神奇的萝卜丝  阅读(28)  评论(0)    收藏  举报