I got "Aborted due to a timeout error" when I ran my code only for some specific test cases. Although my code has been successfully compiled for other test cases. Can someone help me with this?
Link - https://www.hackerrank.com/challenges/phone-book
Formulation of the problem :
You are given a phone book, which consists of the names of people and their phone number. After that, you will be given the name of the person as a request. For each request, print the phone number of this person.
Input format :
The first line will contain an integer indicating the number of entries in the phone book. Each entry consists of two lines: a name and a corresponding phone number.
After that there will be some queries. Each request will contain the name of the person. Read requests to the end of the file.
Limitations:
1 <= N <= 100000
1 <= Request <= 100000
A person’s name consists only of lowercase English letters and can be in the format “name-surname” or in the format “name-name”. Each phone number has exactly 8 digits with no leading zeros.
Output format:
For each case, print "Not Found" if the person does not have an entry in the phone book. Otherwise, type the person’s name and phone number. See sample output for exact format.
, . , .
:
import java.util.*;
import java.io.*;
class Solution
{
public static void main(String []args)
{
Scanner in = new Scanner(System.in);
int n=in.nextInt();
in.nextLine();
ArrayList<String> name = new ArrayList<String>();
int[] phone = new int[100000];
for(int i=0;i<n;i++)
{
name.add(in.nextLine());
phone[i]=in.nextInt();
in.nextLine();
}
while(in.hasNext())
{
String s=in.nextLine();
int a=name.indexOf(s);
if(a>=0)
{
System.out.println(s + "=" + phone[a] );
}
else
{
System.out.println("Not found");
}
}
}
}
PS: . Java. , , :(. , :)