LeetCode Entry

1768. Merge Strings Alternately

18.04.2023 easy 2023 kotlin

we can use sequence zip operator

1768. Merge Strings Alternately easy


fun mergeAlternately(word1: String, word2: String): String =
(word1.asSequence().zip(word2.asSequence()) { a, b -> "$a$b" } +
word1.drop(word2.length) + word2.drop(word1.length))
.joinToString("")

blog post substack

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/184

Intuition

Do what is asked. Handle the tail.

Approach

  • we can use sequence zip operator
  • for the tail, consider drop

    Complexity

  • Time complexity: \(O(n)\)
  • Space complexity: \(O(n)\)