Skip to content

Commit

Permalink
Merge pull request #180 from rasik210/sparsearrays
Browse files Browse the repository at this point in the history
 Sparse arrays problem: C# solution
  • Loading branch information
RyanFehr authored Jul 6, 2018
2 parents 2e47900 + d5c702d commit 38ef126
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
50 changes: 50 additions & 0 deletions DataStructures/Arrays/Sparse Arrays/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Problem: https://www.hackerrank.com/challenges/sparse-arrays/problem
C# Language Version: 6.0
.NET Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts (Key points in algorithm) :
- Going via sparse matrix route the complexity comes out to be O(n * q * 20) so I solved it with help of hash map which gives better complexity
- Scan each input string and stores its count in a hash map.
- For each query string, check its count in the hash map and print it on console.
Gotchas:
<None>
Time Complexity: O(n + q) //n is number of input strings and q is number of queries.
Space Complexity: O(n + q) //a hash map (for storing frequency) and an array (for storing output counts) is addtional space requirement.
*/

using System;
using System.Collections.Generic;

class Solution
{
static void Main(string[] args)
{
var inputCount = int.Parse(Console.ReadLine());
var stringMap = new Dictionary<string, int>();

for (var i = 0; i < inputCount; i++)
{
var input = Console.ReadLine();
if (stringMap.ContainsKey(input))
stringMap[input]++;
else
stringMap.Add(input, 1);
}

var queryCount = int.Parse(Console.ReadLine());
var output = new int[queryCount];
for (var i = 0; i < queryCount; i++)
{
var queryString = Console.ReadLine();
if (stringMap.ContainsKey(queryString))
output[i] = stringMap[queryString];
}

foreach (var item in output)
Console.WriteLine(item);
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@
-----|---------------- |:---------------:| --------------- | --------------- |:-------------:|:--------------:| -----
| |[Arrays - DS](https://www.hackerrank.com/challenges/arrays-ds/problem)| <ul><li>[C++](./DataStructures/Arrays/Arrays%20-%20DS/Solution.cpp)</li><ul> | _O(n)_ | _O(n)_| Easy | 10 | ||
| |[2D Array - DS](https://www.hackerrank.com/challenges/2d-array/problem)| <ul><li>[C++](./DataStructures/Arrays/2D%20Array%20-%20DS/Solution.cpp)</li><ul> | _O(1)_ | _O(1)_| Easy | 15 | ||
| |[Sparse Arrays](https://www.hackerrank.com/challenges/sparse-arrays/problem)| <ul><li>[C#](./DataStructures/Arrays/Sparse%20Arrays/Solution.cs)</li><ul> | _O(n + q)_ | _O(n + q)_| Medium | 25 | n = number of input strings, q = number of queries ||
| |[Dynamic Array](https://www.hackerrank.com/challenges/dynamic-array/problem)| <ul><li>[C#](./DataStructures/Arrays/Dynamic%20Array/Solution.cs)</li><ul> | _O(q)_ | _O(n)_| Easy | 15 | q = Number of queries ||

### Linked Lists
Expand Down

0 comments on commit 38ef126

Please sign in to comment.