Sort by

recency

|

1025 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
    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;
      
    

    }

  • + 0 comments
        List<Character> list = new ArrayList<>();
    
        for(int i=0; i < arr.get(0).length(); i++){
            if(!list.contains(arr.get(0).charAt(i))) 
                            list.add(arr.get(0).charAt(i));
        }
    
        for(int i=1; i <= arr.size()-1; i++){
            String ch = arr.get(i);
    
            for(int j = 0; j < list.size(); j++){
                if(!ch.contains(list.get(j)+"")){ list.remove(j);  j--;
                }
            }
        }
    
        return list.size();
    
  • + 0 comments

    for Python3 Platform

    the best logic is to use intersection between two sets

    from functools import reduce
    
    def gemstones(arr):
        return len(reduce(lambda x, y: x & y, map(set, arr)))
    
    n = int(input())
    arr = []
    for i in range(n):
        arr.append(input())
    
    result = gemstones(arr)
    
    print(result)