Skip to content

344. Reverse String #121

Answered by mah-shamim
mah-shamim asked this question in Q&A
Jul 27, 2024 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

To solve this problem, we can follow these steps:

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

<?php
function reverseString(&$s) {
    $left = 0;
    $right = count($s) - 1;

    while ($left < $right) {
        // Swap the characters
        $temp = $s[$left];
        $s[$left] = $s[$right];
        $s[$right] = $temp;

        // Move the pointers
        $left++;
        $right--;
    }
}

// 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"]
?>

Explanation:

  1. Initialization:

    • Set tw…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Aug 26, 2024
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants