Editorial for Chef and Secret Ingredients [ CHEFING ]

  1. Actually this problem is very simple
    if we use set() function of
    respective language.

  2. You just need to know the very basics of set theory.

  3. Here we have to find those ingredients (actually the characters) which are common in all of the given dishes (actually the strings) .

  4. So actually we need to find the intersection of all the inputs considering the characters in the input dish(actually a string) as the elements of the set.

  5. And then we need to find the length of the set.

Python Code for the given method

#iterate through testcases
for _ in range(int(input())):
    #input N <- number of dishes
    N=int(input())

    #input the first dish as a set and name it arr
    arr=set(input())

    #iterate for N-1 times for getting the other dishes
    for i in range(N-1):
	    # take other dishes as set arr2 in each iteration
	    arr2=set(input())

	    #save in arr the value of arr (intersection) arr2
	    arr=arr.intersection(arr2)

    # finally after all iterations output the number of elements in set arr
    print(len(arr))
1 Like

actually I used a smaller code
check my submission - Python Solution

Instead we can use a character map and keep updating the character map for each new string and it’s alphabet encountered. – C++ Solution