Leetcode 557 Solution

This article provides solution to leetcode question 557 (reverse-words-in-a-string-iii)

https://leetcode.com/problems/reverse-words-in-a-string-iii

Solution

class Solution {
public:
    string reverseWords(string s) {
        int l = 0;

        while (l < s.size())
        {
            int r = l + 1;

            while (s[r] != ' ' && r != s.size())
                r++;
            int next = r + 1;
            r--;

            while (l < r)
                swap(s[l++], s[r--]);

            l = next;
        }

        return s;
    }
};