2

Here are our strings:

const a = "your majesty they are ready";
const b = "your highness they are ready";

I want to get one array containing the longest exact matches between two strings.

So for the first string, we would get:

[ ["your"], ["they are ready"] ]

NOT this result which gets the shortest matches:

[ ["your"], ["they"], ["are"], ["ready"] ]

Getting a word match is an easy task for me. But I need a hand to figure out a proper solution the get the longest possible exact matches...

And by the way I'm using this function to check the exact matches:

// exactMatch("I was sent","sent") returns true And exactMatch("I was sentt","sent") returns false
function exactMatch(ref,compare) {
    return new RegExp("\\b" + ref + "\\b").test(compare);
}

Note: I don't want just the longest match... I want all longest possible matches.

Sara Ree
  • 3,417
  • 12
  • 48

1 Answers1

2

Something like this:

function exactMatch(a, b) {
  a = a.split(' ');
  b = b.split(' ');
  let s = [];
  let out = [];
  let x = a.map(x => b.includes(x) ? x : null);
  x.forEach((v) => {
    if (v == null) {    
    out.push(s);
      s = [];
    } else {
      s.push(v);
    }
  });
  out.push(s);
  out = out.map(x => x.join(' ')).filter(x => x);
  return out;
}
let a = "your majesty they are ready";
let b = "your highness they are ready";
let c = exactMatch(a, b);
console.log(c);

a = "your majesty they are ready, but this is another test";
b = "your highness they are ready, yet this is another test";
c = exactMatch(a, b);
console.log(c);

a = "hello your majesty they are ready, but this is another test";
b = "hi your highness they are ready, yet this is another test";
c = exactMatch(a, b);
console.log(c);

a = "hello your majesty they are ready, but this is another test made";
b = "hi your highness they are ready, yet this is another test completed";
c = exactMatch(a, b);
console.log(c);
  • It is wrong, try with a repeated word `let a = "your majesty they are ready and they are here for us"; let b = "your highness they are ready ands they are here for us";` – Greedo Oct 06 '20 at 12:33
  • 2
    Nice, the solution i was writing was really similar :) – Greedo Oct 06 '20 at 12:44
  • Well...Done...,, – Sara Ree Oct 06 '20 at 13:51
  • @Peter Darmis : Please answer this question too: honestly how did you reach to this mastered level in javascript... is there any resource you advice me to use be able to use simple javascript syntax to work with this complex data processing issues? – Sara Ree Oct 06 '20 at 13:59
  • @SaraRee thank you, i don't know if i have mastered `Javascript`, i try to learn everyday. The good thing is that i do learn everyday something new. This website has been a good start and not only. In addition there are more websites that offer challenges and tests (hackerrank, codility) that you could do online. I also enjoy reading MDN documentation, a lot of interesting articles on Google Developers and great examples regarding `CSS` in css-tricks.com. And not only those. The internet is a vast library, you can find everything. –  Oct 07 '20 at 04:04