All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1301_1400.s1324_print_words_vertically.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g1301_1400.s1324_print_words_vertically;

// #Medium #Array #String #Simulation #2022_03_19_Time_1_ms_(90.59%)_Space_42.3_MB_(43.21%)

import java.util.ArrayList;
import java.util.List;

/**
 * 1324 - Print Words Vertically\.
 *
 * Medium
 *
 * 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:**
 *
 * *   `1 <= s.length <= 200`
 * *   `s` contains only upper case English letters.
 * *   It's guaranteed that there is only one space between 2 words.
**/
public class Solution {
    public List printVertically(String s) {
        String[] words = s.split(" ");
        int columnMax = 0;
        for (String word : words) {
            columnMax = Math.max(columnMax, word.length());
        }
        char[][] matrix = new char[words.length][columnMax];
        for (int i = 0; i < words.length; i++) {
            int j = 0;
            for (; j < words[i].length(); j++) {
                matrix[i][j] = words[i].charAt(j);
            }
            while (j < columnMax) {
                matrix[i][j++] = '#';
            }
        }
        List result = new ArrayList<>();
        for (int j = 0; j < columnMax; j++) {
            StringBuilder sb = new StringBuilder();
            for (char[] chars : matrix) {
                if (chars[j] != '#') {
                    sb.append(chars[j]);
                } else {
                    sb.append(' ');
                }
            }
            String str = sb.toString();
            int k = str.length() - 1;
            while (k >= 0 && str.charAt(k) == ' ') {
                k--;
            }
            result.add(str.substring(0, k + 1));
            sb.setLength(0);
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy