package fundamentals.string.manipulate.lc186_reversewordsinastring2;

/**
 * Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.
 * The input string does not contain leading or trailing spaces and the words are always separated by a single space.
 */
public class Solution {

    public static void main(String[] args) {
        char[] s = "the sky is blue".toCharArray();
        new Solution().reverseWords(s);
        System.out.println(String.valueOf(s));
    }

    public void reverseWords(char[] s) {
        int n = s.length;
        swap(s, 0, n - 1);
        for (int i = 0, j = 0; j <= n; j++) {
            if (j == n || s[j] == ' ') {
                swap(s, i, j - 1);
                i = j + 1;
            }
        }
    }

    private void swap(char[] s, int i, int j) {
        for (; i < j; i++, j--) {
            char tmp = s[i];
            s[i] = s[j];
            s[j] = tmp;
        }
    }

}

@@ -63,16 +63,93 @@
 ring2;%0A%0A
+import org.junit.jupiter.api.Assertions;%0Aimport org.junit.jupiter.api.Test;%0A%0A
 /**%0A * G
@@ -415,48 +415,172 @@
     
-public static void main(String%5B%5D args) %7B
+@Test%0A    void testNormal() %7B%0A        char%5B%5D ch1 = %22hello%22.toCharArray();%0A        reverseWords(ch1);%0A        Assertions.assertEquals(%22hello%22, String.valueOf(ch1));%0A
 %0A   
@@ -591,17 +591,19 @@
  char%5B%5D 
-s
+ch2
  = %22the 
@@ -642,23 +642,8 @@
     
-new Solution().
 reve
@@ -651,17 +651,19 @@
 seWords(
-s
+ch2
 );%0A     
@@ -669,27 +669,51 @@
     
-System.out.println(
+Assertions.assertEquals(%22blue is sky the%22, 
 Stri
@@ -723,17 +723,19 @@
 valueOf(
-s
+ch2
 ));%0A    
@@ -808,28 +808,31 @@
 th;%0A        
-swap
+reverse
 (s, 0, n - 1
@@ -937,20 +937,23 @@
         
-swap
+reverse
 (s, i, j
@@ -1038,12 +1038,15 @@
 oid 
-swap
+reverse
 (cha

reverse words in a string II