Combine strings and remove duplicates in Python

The challenge

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.

Examples:

a = "xyaabbbccccdefww" b = "xxxxyyyyabklmopq" longest(a, b) -> "abcdefklmopqwxy" a = "abcdefghijklmnopqrstuvwxyz" longest(a, a) -> "abcdefghijklmnopqrstuvwxyz"
Code language: Python (python)

The solution in Python code

Option 1 (Using a Dictionary):

def longest(a1, a2): a3 = list(dict.fromkeys(sorted(a1+a2))) return "".join(a3)
Code language: Python (python)

Option 2 (Using a Set):

def longest(a1, a2): return "".join(sorted(set(a1 + a2)))
Code language: Python (python)

Option 3 (Using a Union):

def longest(s1, s2): return ''.join(sorted(set(s1).union(s2)))
Code language: Python (python)

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")
Code language: Python (python)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments