LeetCode Entry

344. Reverse String

02.06.2024 easy 2024 kotlin rust

Reverse an array

344. Reverse String easy blog post substack youtube 2024-06-02_08-19.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/626

Problem TLDR

Reverse an array #easy

Intuition

We can use two pointers or just a single for-loop until the middle.

Approach

  • Careful with the corner case: exclude the middle for the even size
  • try to use built-in functions

Complexity

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

  • Space complexity: \(O(1)\)

Code


    fun reverseString(s: CharArray) = s.reverse()


    pub fn reverse_string(s: &mut Vec<char>) {
        s.reverse()
    }