You are not logged in. Please login at www.codechef.com to post your questions!

×

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))

asked 13 Feb, 17:25

pratyaydeep's gravatar image

2★pratyaydeep
112
accept rate: 0%

edited 15 Feb, 00:28


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

link

answered 13 Feb, 17:59

pratyaydeep's gravatar image

2★pratyaydeep
112
accept rate: 0%

edited 13 Feb, 18:00

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

link

answered 15 Feb, 13:23

pranjal01shi's gravatar image

2★pranjal01shi
11
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,852
×427
×189
×13
×8

question asked: 13 Feb, 17:25

question was seen: 322 times

last updated: 15 Feb, 13:23