Sort by

recency

|

1047 Discussions

|

  • + 0 comments

    Here is a O(n+m) time and O(1) space solution in python.

    O(n+m) is the average of the worse case scenarion, where all 26 chars will be present in every string. This means in the worse case the time will be O(Ch*n).

    def gemstones(arr):
        res = set(arr[0])
        
        for i in range(1, len(arr)):
            res &= set(arr[i])
        
        return len(res)
    
  • + 0 comments

    C# solution:

    public static int gemstones(List<string> arr)
        {
            HashSet<char> common = new HashSet<char>(arr[0]);
            for (int i = 1; i < arr.Count; i++)
            {
                common.IntersectWith(arr[i]);
            }
            return common.Count;
        }
    
  • + 0 comments
    def gemstones(arr):
        # Write your code here
        common = set(arr[0])
        
        for i in range(1, len(arr)):
            common &= set(arr[i])
            
        return len(common)
    
  • + 0 comments
    def gemstones(arr):
        # Write your code here
        gemstones = 0
        map1 = {}
        
        for item in arr:
            map2 = {}
            for i in item:
                if map1.get(i):
                    if not map2.get(i):
                        map1[i] += 1
                        map2[i] = 1
                    else:
                        continue
                    if map1[i] == len(arr):
                        gemstones += 1
                else:
                    map1[i] = 1
                    map2[i] = 1
                
        return gemstones
    
  • + 0 comments

    Here is problem solution in python java c++ c and javascript - https://programmingoneonone.com/hackerrank-gemstones-problem-solution.html