Skip to main content
Tweeted twitter.com/StackCodeReview/status/879255791047409664
swapped timeout tag for time-limit-exceeded
Link
Graipher
  • 41.1k
  • 7
  • 69
  • 133
Source Link

Terminated due to timeout Mapping phone numbers

I have a code based on the following problem "Mapping phone numbers to names", the problem is that when they are going to process large data(+100.000) the program ends by timeout. my code and problem is this, how can I optimize my code to consume less time?

my code

function processData(input) {
  var r=input.split(/\n/),o=Number(r[0]),c=r.length-1,x=[];
  r.splice(0,o+1);
  r.forEach((l)=>{
      var rx=new RegExp(l+"\\s(\\d+)"),o=input.match(rx);
      console.log(o==null?"Not found":l+"="+o[1]);
  });
} 

Example Input

3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry

Example Output

sam=99912222
Not found
harry=12299933

The program generates timeout in the following case

BIG Example Input and output Thanks