Sort by

recency

|

1027 Discussions

|

  • + 0 comments

    Java 15

    public static int gemstones(List<String> arr) {
        // Write your code here
        int count=0; 
        String alphabet = "abcdefghijklmnopqrstuvwxyz";
        for (int i = 0; i < alphabet.length(); i++) {
            boolean flag = true;
            for (String str : arr) {
                if(!str.contains(String.valueOf(alphabet.charAt(i)))){
                    flag = false;
                }
            }
            if (flag) {
                count++;
            }
        }
        
        return count;
    
        }
    
  • + 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

    Python

    def gemstones(arr):
        sets = [set(i) for i in arr]
        return len(set.intersection(*sets))
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner input = new Scanner(System.in);
            int n = input.nextInt();
            input.nextLine();
            
            Set<Character> gemstones = stringToSet(input.nextLine()); //Set of gemstones
            
            for(int i=1; i<n ;i++){
                gemstones.retainAll(stringToSet(input.nextLine())); //Perform intersection
            }
            System.out.print(gemstones.size());
        }
        
        
        
        
        public static Set<Character> stringToSet(String s) //Converts String to Character set
        {
            Set<Character> set = new HashSet<Character>(26);
            for (char c : s.toCharArray())
                set.add(Character.valueOf(c));
            return set;
        }
    }
    
  • + 0 comments

    JS:

    function gemstones(arr) {
        // Write your code here
       let alp=arr[0].split('');
       let newarr=new Set();
       for(let x=0;x<alp.length;x++){
          let found=true;
          for(let i=1;i<arr.length;i++){
           if(arr[i].indexOf(alp[x])==-1){
               found=false;
           }
         } 
         if(found){
             newarr.add(alp[x]);
         }
       }
       console.log(arr,newarr)
       return newarr.size;
      
    

    }