Solution with array of ascii characters


  • 0
    A

    class Solution {
    public:
    bool canConstruct(string ransomNote, string magazine) {
    int ascii[256] = {0};
    for(int i = 0; i<magazine.length(); i++) {
    ascii[magazine[i]]++;
    }
    for(int i = 0; i<ransomNote.length(); i++) {
    ascii[ransomNote[i]]--;
    }
    for(int i = 0; i<256; i++) {
    if(ascii[i]<0)
    return false;
    }
    return true;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.