Sort by

recency

|

1037 Discussions

|

  • + 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;
    }
    
  • + 0 comments
    def gemstones(arr):
        # Write your code here
        # Create a set of characters from the first rock
        gemstones_set = set(arr[0])
        
        # Iterate through the remaining rocks
        for i in range(1, len(arr)):
            # Update the gemstones set with the intersection of current rock's characters
            gemstones_set = gemstones_set.intersection(set(arr[i]))
        
        # Return the number of gemstones (size of the set)
        return len(gemstones_set)
    		
    
  • + 0 comments

    Here is my Python solution!

    def gemstones(arr):
        gems = 0
        rocks = len(arr)
        minerals = set(list("".join(arr)))
        arr = [list(rock) for rock in arr]
        for mineral in minerals:
            if len([True for rock in arr if mineral in rock]) == rocks:
                gems += 1
        return gems
    
  • + 0 comments
    
    
    let contadorPiedrasPreciosas = 0
    let piedras = []
    const rocas = []
    for (const rock of arr) {
        for (const p of rock ) {
            if (piedras.includes(p)) {
                continue;
            } else {
                piedras.push(p)
            }
        }
        rocas.push(piedras)
        piedras = []
    }
    for (const r of rocas) {
        for (const p of r) {
            aparicionPiedras[p] = (aparicionPiedras[p] || 0) + 1 
        }
    }
    for (const p in aparicionPiedras) {
        if (aparicionPiedras[p] === arr.length) {
            contadorPiedrasPreciosas++;
        }
    }
    return contadorPiedrasPreciosas``
    

    `

  • + 0 comments
    int gemstones(int arr_count, char** arr) {
        char* ok = NULL;
        int pres = 0, dragon = 0;
        for(char i = 'a';i <= 'z';i++) {
            for(int j = 0;j < arr_count;j++) {
                ok = strchr(arr[j],i);
                if(ok != NULL) {
                    pres++;
                }
                ok = NULL;
            }
            if(pres == arr_count) {
                dragon++;
            }
            pres = 0;
        }
        free(ok);
        return dragon;