나는 이렇게 학습한다/Algorithm & SQL

Two to One

daco2020 2022. 4. 26. 21:52
반응형

Description:

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"

 

 

Solution:

1. Concatenate strings.
2. Convert the string into an array.
3. Duplicate elements are removed.
4. Sort in ascending order.
5. Return the array as a string.

 

def longest(a1, a2):
    return "".join(sorted(set(a1 + a2)))

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Sum of odd numbers  (0) 2022.04.28
Mumbling  (0) 2022.04.27
Apparently-Modifying Strings  (0) 2022.04.26
The Office I - Outed  (0) 2022.04.24
Shortest Word  (0) 2022.04.23