DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Discussion on: A coding interview question asked at Google

Collapse
 
rascanoo profile image
rascanoo • Edited on
Using Regex:

const check = arr => {
  let regex = /[a-z]-B/g

  let [x, y] = arr.map(elm => {
    elm = elm.replace(/,/g, '');

    while (regex.test(elm)) {
      elm = elm.replace(regex, '');
    }

    return elm.replace(/-B/g, '');
  });

  return x === y;
}

console.log(check(["a,b,c,d", "a,b,c,c,-B,d"])); // true
console.log(check(["-B,-B,-B,c,c", "c,c"])); // true
console.log(check(["", "a,-B,-B,a,-B,a,b,c,c,c,d"])); // false
console.log(check(["a,a,a,-B,-B,c", "a,c"])); //true
Collapse
 
gabgab2003 profile image
DownloadPizza

This is actually awesome, it shouldn't take long to make this into something that works with multiple lines

Collapse
 
hyftar profile image
Simon Landry

I'm not convinced this works with every case; You're missing some edge cases such as

['a,a,a,-B,-B,c', 'a,c']
Collapse
 
rascanoo profile image
rascanoo

Hi Simon,

You are correct, well spotted. I changed the solution to reflect your case and hopefully all cases :-)
Couldn't do it with one regex though :-(