package string;

import java.util.Stack;

/**
 * Created by Yue on 2017/10/19.
 * leetcode no.557
 */
public class ReverseWordsInAStringIII {
    public static void main(String[] args) {
        String s = "Let's take LeetCode contest";
        System.out.println(reverseWords(s));
    }

    public static String reverseWords(String s) {
        Stack<Character> stack = new Stack<>();
        char[] cs = s.toCharArray();
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < cs.length;  i++) {
            if (cs[i] != ' ' && i != cs.length - 1)
                stack.push(cs[i]);
            else {
                while (!stack.empty())
                    res.append(stack.pop());
                res.append(' ');
            }
        }
        return res.toString();
    }
}

@@ -545,32 +545,10 @@
  ' '
- && i != cs.length - 1
 )%0A
+
     
@@ -594,12 +594,47 @@
     
-else
+if (cs%5Bi%5D == ' ' %7C%7C i == cs.length - 1)
  %7B%0A 
@@ -712,24 +712,68 @@
 ack.pop());%0A
+                if (i != cs.length - 1)%0A    
             

modify leetcode no.557