Skip to content

Latest commit

 

History

History
26 lines (22 loc) · 770 Bytes

longest.md

File metadata and controls

26 lines (22 loc) · 770 Bytes
  • Take 2 strings s1 and s2 including only letters from ato z. Return a new sorted string, the longest possible, containing distinct letters - each taken only once - coming from s1 or s2.
  • Return any array that satisfies this condition.

Samples:

a = "xyaabbbccccdefww" 
b = "xxxxyyyyabklmopq" 
longest(a, b) -> "abcdefklmopqwxy" 
c = "abcdefghijklmnopqrstuvwxyz"; 
longest(c, c) -> "abcdefghijklmnopqrstuvwxyz"

Solution:

function longest(s1, s2) {
  let x = s1.concat(s2);
  let y = x.split("").sort();

  let z = new Set(y);
  return Array.from(z).join("");
}

console.log(longest(a, b)); // abcdefklmopqwxy
console.log(longest(c, c)); // abcdefghijklmnopqrstuvwxyz