The challenge
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"
The solution in Python code
Option 1 (Using a Dictionary):
def longest(a1, a2):
a3 = list(dict.fromkeys(sorted(a1+a2)))
return "".join(a3)
Option 2 (Using a Set):
def longest(a1, a2):
return "".join(sorted(set(a1 + a2)))
Option 3 (Using a Union):
def longest(s1, s2):
return ''.join(sorted(set(s1).union(s2)))
Test cases to validate our solution
import test as test
@test.describe("longest")
def tests():
@test.it("basic tests")
def basics():
test.assert_equals(longest("aretheyhere", "yestheyarehere"), "aehrsty")
test.assert_equals(longest("loopingisfunbutdangerous", "lessdangerousthancoding"), "abcdefghilnoprstu")
test.assert_equals(longest("inmanylanguages", "theresapairoffunctions"), "acefghilmnoprstuy")