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

1127. Two to One

daco2020 2022. 11. 27. 22:29
반응형

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:

String longest(String a, String b) {
  List result = (a + b).split('').toSet().toList();
  result.sort();
  return result.join('');
}
String longest(String a, String b) {
  List<String> list = [...a.split(''), ...b.split('')].toSet().toList()..sort();
  return list.join();
}
longest(a, b) => (((a + b).split('').toSet().toList())..sort()).join();


반응형

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

1129. Bin to Decimal  (0) 2022.11.29
1128. Hex to Decimal  (0) 2022.11.28
1126. How good are you really?  (0) 2022.11.26
1125. MakeUpperCase  (0) 2022.11.25
1124. Beginner Series #2 Clock  (0) 2022.11.24