GemsStone problem


/**
 John has discovered various rocks. Each rock is composed of various elements, and each element is represented by
 a lower-case Latin letter from 'a' to 'z'. An element can be present multiple times in a rock. An element is called a
 gem-element if it occurs at least once in each of the rocks.
Given the list of N rocks with their compositions, display the number of gem-elements that exist in those rocks.
Input Format
The first line consists of an integer,N , the number of rocks.
Each of the next N lines contains a rock's composition. Each composition consists of lower-case letters of English alphabet.
Constraints
1<=N<=100
Each composition consists of only lower-case Latin letters ('a'-'z').
1<= length of each composition <=100
Output Format
Print the number of gem-elements that are common in these rocks. If there are none, print 0.
Sample Input
3
abcdde
baccd
eeabg
Sample Output
2
Explanation
Only "a" and "b" are the two kinds of gem-elements, since these are the only characters that occur in every rock's composition.
 */



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HackerDemo
{
    class Gemstones
    {
        public void GemstonesMain()
        {
            int n = Convert.ToInt32(Console.ReadLine());
            List<string> listInputStr = new List<string>();
            for (int a0 = 0; a0 < n; a0++)
            {
                listInputStr.Add(Console.ReadLine()); 
            }
            string shortstr = listInputStr.OrderBy(s => s.Length).First();
            List<char> charList = GetCommonChars(shortstr, listInputStr).Distinct().ToList();
            Console.WriteLine(charList.Count);
        }

        public List<char> GetCommonChars(string shortStr, List<string> strList)
        {
            List<char>charList = new List<char>();
            charList = shortStr.ToCharArray().ToList();
            for (int i = 0; i < shortStr.Length; i++)
            {
                foreach (var str in strList)
                {
                    if (!str.Contains(shortStr[i]))
                    {
                        charList.Remove(shortStr[i]);
                    }
                }
            }

            return charList;
        }
    }
}

No comments:

Post a Comment