I am learning Data Structures and I am stuck in a problem I cannot find the way to improve the performance of the code. The problem is this: https://www.hackerrank.com/challenges/contacts/problem I made the solution and I could pass 3 of the 15 tests, but the rest I couldn´t because show me:
Time limit exceeded Your code did not execute within the time limits. Please optimize your code. For more information on execution time limits, refer to the environment page
I change the nested if with linq, but I couldn't get the improvement. Could you help me? I left the code I am using:
public static List<int> contacts (List<List<string>> queries)
{
List<string> contactList = new List<string>();
List<string> findList = new List<string>();
List<int> result = new List<int>();
foreach (var instruction in queries)
{
if (instruction[0] == "add")
contactList.Add(instruction[1]);
else
findList.Add(instruction[1]);
}
for(int i = 0; i < findList.Count; i++)
{
//-----------------------------------------------------------------------
var counter = contactList.Where(x => x.Contains(findList[i])).Count();
result.Add(counter);
//-----------------------------------------------------------------------
//foreach (var contact in contactList)
//{
// if (contact.Contains(findList[i]))
// result[i]++;
//}
}
return result;
}
Thanks in advance for your help