Two to One

Lee·2022년 7월 6일

Algorithm

목록 보기
39/92
post-thumbnail

❓ Two to One

Q. Take 2 strings s1 and s2 including only letters from a to z. Return a new sorted string, the longest possible, containing distinct letters - each taken only once - coming from s1 or s2.

Examples:
a = "xyaabbbccccdefww"
b = "xxxxyyyyabklmopq"
longest(a, b) -> "abcdefklmopqwxy"

a = "abcdefghijklmnopqrstuvwxyz"
longest(a, a) -> "abcdefghijklmnopqrstuvwxyz"

✔ Solution

//#my solution
function longest(s1, s2) {
  let result = [...s1, ...s2];
  result.sort();
  let long = [];
  result.forEach((ele) => {
    if (!long.includes(ele)) {
      long.push(ele);
    }
  });
  return long.join("");
}


//#other solution
const longest = (s1, s2) => [...new Set(s1+s2)].sort().join('')
profile
Lee

0개의 댓글