DEV Community

Cover image for 344. Reverse String
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on • Edited on

1

344. Reverse String

344. Reverse String

Difficulty: Easy

Topics: Two Pointers, String

Write a function that reverses a string. The input string is given as an array of characters s.

You must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

  • Input: s = ["h","e","l","l","o"]
  • Output: ["o","l","l","e","h"]

Example 2:

  • Input: s = ["H","a","n","n","a","h"]
  • Output: ["h","a","n","n","a","H"]

Constraints:

Hint:

  1. The entire logic for reversing a string is based on using the opposite directional two-pointer approach!

Solution:

To solve this problem, we can follow these steps:

Let's implement this solution in PHP: 344. Reverse String

<?php
/**
 * @param String[] $s
 * @return NULL
 */
function reverseString(&$s) {
    ...
    ...
    ...
    /**
     * go to ./solution.php
     */
}

// Example 1:
$s1 = ["h", "e", "l", "l", "o"];
reverseString($s1);
print_r($s1); // Output: ["o", "l", "l", "e", "h"]

// Example 2:
$s2 = ["H", "a", "n", "n", "a", "h"];
reverseString($s2);
print_r($s2); // Output: ["h", "a", "n", "n", "a", "H"]
?>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Initialization:

    • Set two pointers: left starting from the beginning (0) and right from the end (count($s) - 1).
  2. Swapping:

    • Swap the elements at left and right positions.
    • Increment the left pointer and decrement the right pointer after each swap.
  3. Loop:

    • Continue the swapping until left is less than right.
  4. In-Place Modification:

    • The input array $s is modified in place, meaning the original array is reversed without using any additional arrays.

This solution has a time complexity of O(n) and a space complexity of O(1), as required.

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

Billboard image

The fastest way to detect downtimes

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitoring.

Get started now

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay