Posts Reverse words in a given string (geeksforgeeks - SDE Sheet)
Post
Cancel

Reverse words in a given string (geeksforgeeks - SDE Sheet)

PROBLEM DESCRIPTION

Given a String S, reverse the string without reversing its individual words. Words are separated by dots.

geeksforgeeks

SOLUTION

A simple way to solve this is by using split() method. Remember to escape the special dot character . 🙃

Here, we will solve this without using split(). The idea is simple, maintain two pointers - one to mark the start and another to mark the end of any word. We can start from the right of the string. Keep looking for the next dot while shifting the left pointers towards left. When the left pointers meets the dot, we can say that the word is from index [left+1, right]. For the next word, decrease left by 1 and reset right to the same position as left.

Continue doing this until left is less than 0 (no more characters left).

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
class Solution
{
    String reverseWords(String S)
    {
        int n = S.length();

        // StringBuffer to store the reversed string
        StringBuffer sb = new StringBuffer();

        // Initialize pointers for traversing the string
        int left = n - 1; // Points to the end of the current word
        int right = n - 1; // Points to the end of the string

        // Loop until we have processed the entire string
        while (left >= 0) {
            // Move left pointer until we reach the beginning of the current word
            while (left >= 0 && S.charAt(left) != '.') {
                left--;
            }

            // Append the current word (from left to right) to the StringBuffer
            sb.append(S.substring(left + 1, right + 1) + ".");

            // Move left pointer one step back to skip the dot
            left--;
            // Update right pointer to the new end of the next word
            right = left;
        }

        // Convert StringBuffer to String and remove the trailing dot
        return sb.toString().substring(0, sb.length() - 1);
    }
}
This post is licensed under CC BY 4.0 by the author.