HackerRank Two Strings | JS Solution

Problem

HackerRank detailed problem description can be found here.

Inputs & Outputs

/*
param {string} s1
param {string} s1

returns {string} 'YES' || 'NO'
*/

Test Case

twoStrings('and', 'art');
twoStrings('be', 'cat');

JavaScript Solution

function twoStrings(s1, s2) {
  let lettersMap = {};
  for (const letter of s1) {
    if (!lettersMap[letter]) {
      lettersMap[letter] = 1;
    }
  }

  for (const letter of s2) {
    if (lettersMap[letter]) {
      return 'YES';
    }
  }
  return 'NO';
}

Resources

  1. Two Strings algorithm by HackerRank
  2. JavaScript Object Properties by W3Schools