Sparse Arrays Solved!
https://www.hackerrank.com/challenges/sparse-arrays?utm_campaign=challenge-recommendation&utm_medium=email&utm_source=24-hour-campaign
The main idea is to add string n in a dictionary's key and update count in the dictionary's value.
Then we just need to check query against the dictionary and return its value.
The main idea is to add string n in a dictionary's key and update count in the dictionary's value.
Then we just need to check query against the dictionary and return its value.
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
var nStrings = Int32.Parse(Console.ReadLine());
var dic = new Dictionary();
for(int i = 0; i < nStrings; i++)
{
var wrd = Console.ReadLine();
if (dic.ContainsKey(wrd))
dic[wrd]++;
else
dic.Add(wrd,1);
}
var qStrings = Int32.Parse(Console.ReadLine());
for(int i = 0; i < qStrings; i++)
{
var inputS = Console.ReadLine();
Console.WriteLine(dic.ContainsKey(inputS)? dic[inputS] : 0);
}
}
}
Comments