DEV Community

Minh Hieu
Minh Hieu

Posted on

Gemstones Explain - Solution | Javascript

function gemstones(arr) {
  const first = arr[0];
  const checked = {};
  let result = 0;
  for(let i = 0; i < first.length; i++) {
    if (checked[first[i]]) continue;
    checked[first[i]] = true;

    const regex = new RegExp(first[i], 'i');
    let temp = 0;
    for (let j = 1; j < arr.length; j ++) {
      console.log(arr[j], regex.test(arr[j]));
      if (regex.test(arr[j])) {
        temp ++;
      } else {
        break;
      }
    }
    if (temp === (arr.length - 1)) {
result ++;
    }

  }
  return result;
}

gemstones(['abcdde', 'baccd', 'eeabg']);

Problem

Top comments (0)