• + 0 comments

    Here are my c++ approaches of solving this problem, video explanation here : https://youtu.be/EjhdJXN3a3c

    Solution 1 : Using a list of all possible characters

    int gemstones(vector<string> arr) {
        int result = 0;
        string liste = "abcdefghijklmnopqrstuvwxyz";
        for(int j = 0; j < liste.size(); j++){
            int cp = 0;
            for(int i = 0; i < arr.size(); i++){
                if(arr[i].find(liste[j]) != string::npos) cp++;
            }
            if(cp == arr.size())result++;
        }
        return result;
    }
    

    Solution 2 : Using set of the shortest element of the array of string

    string getShortest(vector<string> arr) {
        string result = arr[0];
        for(int i = 1; i < arr.size(); i++){
            if(arr[i].size() < result.size()) result = arr[i];
        }
        return result;
    }
    int gemstones(vector<string> arr) {
        int result = 0;
        string shortest = getShortest(arr);
        set<char> liste(shortest.begin(), shortest.end());
        for(auto j = liste.begin(); j != liste.end(); j++){
            int cp = 0;
            for(int i = 0; i < arr.size(); i++){
                if(arr[i].find(*j) != string::npos) cp++;
            }
            if(cp == arr.size())result++;
        }
        return result;
    }