1.10.2023
557. Reverse Words in a String III easy
blog post
Join me on Telegram
https://t.me/leetcode_daily_unstoppable/356
Problem TLDR
Reverse words
Intuition
In an interview, in-place solution expected. Maintain two pointers, and adjust one until end of word reached. This still takes O(N) space in JVM.
Approach
Let’s write a one-liner using Kotlin’s API
Complexity
Time complexity:
O(n)Space complexity:
O(n)
Code
fun reverseWords(s: String) =
s.reversed().split(" ").reversed().joinToString(" ")