Posts Reverse Words in a String II
Post
Cancel

Reverse Words in a String II

PROBLEM DESCRIPTION

Given a character array s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by a single space. Your code must solve the problem in-place, i.e. without allocating extra space.

leetcode

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {

    public void reverseWords(char[] s) {

        int n = s.length;

        // Step 1: Reverse the entire character array
        reverseRange(s, 0, n-1);

        // Initialize pointers i and j for word reversal
        int i=0;
        int j=0;

        // Step 2: Iterate through the character array to reverse individual words
        while(i<n){

            // Move the j pointer to the end of the current word
            while(j<n && s[j] != ' '){
                j++;
            }

            // Step 3: Reverse the current word (from i to j-1)
            reverseRange(s, i, j-1);

            // Move the i and j pointers to the next word
            i = j + 1;
            j++;
        }
    }

    // Helper function to reverse a range of characters in the array
    public void reverseRange(char[] s, int i, int j){
        while(i<j){

            // Swap characters at positions i and j
            char t = s[i];
            s[i] = s[j];
            s[j] = t;

            // Move the pointers towards each other
            i++;
            j--;
        }
    }
}
This post is licensed under CC BY 4.0 by the author.