1324. Print Words Vertically LeetCode Solution

In this guide, you will get 1324. Print Words Vertically LeetCode Solution with the best time and space complexity. The solution to Print Words Vertically problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Print Words Vertically solution in C++
  4. Print Words Vertically solution in Java
  5. Print Words Vertically solution in Python
  6. Additional Resources
1324. Print Words Vertically LeetCode Solution image

Problem Statement of 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:

1 <= s.length <= 200
s contains only upper case English letters.
It's guaranteed that there is only one space between 2 words.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1324. Print Words Vertically LeetCode Solution in C++

class Solution {
 public:
  vector<string> printVertically(string s) {
    vector<string> ans;
    vector<string> words = split(s);
    size_t maxLength = 0;

    for (const string& word : words)
      maxLength = max(maxLength, word.length());

    for (size_t i = 0; i < maxLength; ++i) {
      string row;
      for (const string& word : words)
        row += i < word.length() ? word[i] : ' ';
      while (row.back() == ' ')
        row.pop_back();
      ans.push_back(row);
    }

    return ans;
  }

 private:
  vector<string> split(const string& s) {
    vector<string> words;
    istringstream iss(s);
    for (string token; iss >> token;)
      words.push_back(token);
    return words;
  }
};
/* code provided by PROGIEZ */

1324. Print Words Vertically LeetCode Solution in Java

class Solution {
  public List<String> printVertically(String s) {
    List<String> ans = new ArrayList<>();
    String[] words = s.split(" ");
    int maxLength = 0;

    for (final String word : words)
      maxLength = Math.max(maxLength, word.length());

    for (int i = 0; i < maxLength; ++i) {
      StringBuilder sb = new StringBuilder();
      for (final String word : words)
        sb.append(i < word.length() ? word.charAt(i) : ' ');
      while (sb.charAt(sb.length() - 1) == ' ')
        sb.deleteCharAt(sb.length() - 1);
      ans.add(sb.toString());
    }

    return ans;
  }
}
// code provided by PROGIEZ

1324. Print Words Vertically LeetCode Solution in Python

class Solution:
  def printVertically(self, s: str) -> list[str]:
    ans = []
    words = s.split()
    maxLength = max(len(word) for word in words)

    for i in range(maxLength):
      row = []
      for word in words:
        row.append(word[i] if i < len(word) else ' ')
      ans.append(''.join(row).rstrip())

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.