Print Words Vertically

Given a string s. Return all the words vertically in the same order in which they appear in s.
Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed).
Each word would be put on only one column and that in one column there will be only one word.

 

Example 1:

Input: s = "HOW ARE YOU"
Output: ["HAY","ORO","WEU"]
Explanation: Each word is printed vertically. 
 "HAY"
 "ORO"
 "WEU"

Example 2:

Input: s = "TO BE OR NOT TO BE"
Output: ["TBONTB","OEROOE","   T"]
Explanation: Trailing spaces is not allowed. 
"TBONTB"
"OEROOE"
"   T"

Example 3:

Input: s = "CONTEST IS COMING"
Output: ["CIC","OSO","N M","T I","E N","S G","T"]

 

Constraints:


Solution:

class Solution {
    public List<String> printVertically(String s) {
        List<String> res = new ArrayList();
        String[] arr = s.split(" ");
        int maxLen = 0;
        for (String str : arr) {
            maxLen = Math.max(maxLen, str.length());
        }
        for (int i = 0; i < maxLen; i ++) {
            res.add("");
        }
        for (String str : arr) {
            for (int i = 0; i < maxLen; i ++) {
                String curr = res.get(i);
                if (i < str.length()) {
                    curr = curr + String.valueOf(str.charAt(i));
                } else {
                    curr = curr + " ";
                }
                res.set(i, curr);
            }
        }
        for (int i = 0; i < maxLen; i ++) {
            StringBuilder sb = new StringBuilder(res.get(i));
            while (sb.charAt(sb.length() - 1) == ' ') {
                sb.deleteCharAt(sb.length() - 1);
            }
            res.set(i, sb.toString());
        }
        return res;
    }
}