Skip to content

Commit e1392fc

Browse files
add 1324
1 parent 0f40a9b commit e1392fc

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ _If you like this project, please leave me a star._ ★
2222
|1331|[Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1331.java) | |Easy||
2323
|1329|[Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1329.java) | |Medium||
2424
|1325|[Delete Leaves With a Given Value](https://leetcode.com/problems/delete-leaves-with-a-given-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1325.java) | |Medium|Tree|
25+
|1324|[Print Words Vertically](https://leetcode.com/problems/print-words-vertically/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1324.java) | |Medium|String|
2526
|1323|[Maximum 69 Number](https://leetcode.com/problems/maximum-69-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1323.java) | |Easy|Math|
2627
|1317|[Convert Integer to the Sum of Two No-Zero Integers](https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1317.java) | |Easy||
2728
|1315|[Sum of Nodes with Even-Valued Grandparent](https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1315.java) | |Medium|Tree, DFS|
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 1324. Print Words Vertically
8+
*
9+
* Given a string s. Return all the words vertically in the same order in which they appear in s.
10+
* Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed).
11+
* Each word would be put on only one column and that in one column there will be only one word.
12+
*
13+
* Example 1:
14+
* Input: s = "HOW ARE YOU"
15+
* Output: ["HAY","ORO","WEU"]
16+
* Explanation: Each word is printed vertically.
17+
* "HAY"
18+
* "ORO"
19+
* "WEU"
20+
*
21+
* Example 2:
22+
* Input: s = "TO BE OR NOT TO BE"
23+
* Output: ["TBONTB","OEROOE"," T"]
24+
* Explanation: Trailing spaces is not allowed.
25+
* "TBONTB"
26+
* "OEROOE"
27+
* " T"
28+
*
29+
* Example 3:
30+
* Input: s = "CONTEST IS COMING"
31+
* Output: ["CIC","OSO","N M","T I","E N","S G","T"]
32+
*
33+
* Constraints:
34+
* 1 <= s.length <= 200
35+
* s contains only upper case English letters.
36+
* It's guaranteed that there is only one space between 2 words.
37+
* */
38+
public class _1324 {
39+
public static class Solution1 {
40+
public List<String> printVertically(String s) {
41+
String[] words = s.split(" ");
42+
int columnMax = 0;
43+
for (String word : words) {
44+
columnMax = Math.max(columnMax, word.length());
45+
}
46+
char[][] matrix = new char[words.length][columnMax];
47+
for (int i = 0; i < words.length; i++) {
48+
int j = 0;
49+
for (; j < words[i].length(); j++) {
50+
matrix[i][j] = words[i].charAt(j);
51+
}
52+
while (j < columnMax) {
53+
matrix[i][j++] = '#';
54+
}
55+
}
56+
List<String> result = new ArrayList<>();
57+
for (int j = 0; j < columnMax; j++) {
58+
StringBuilder sb = new StringBuilder();
59+
for (int i = 0; i < matrix.length; i++) {
60+
if (matrix[i][j] != '#') {
61+
sb.append(matrix[i][j]);
62+
} else {
63+
sb.append(' ');
64+
}
65+
}
66+
String str = sb.toString();
67+
int k = str.length() - 1;
68+
while (k >= 0 && str.charAt(k) == ' ') {
69+
k--;
70+
}
71+
result.add(str.substring(0, k + 1));
72+
sb.setLength(0);
73+
}
74+
return result;
75+
}
76+
}
77+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1324;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _1324Test {
12+
private static _1324.Solution1 solution1;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1324.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(Arrays.asList("HAY", "ORO", "WEU"), solution1.printVertically("HOW ARE YOU"));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
assertEquals(Arrays.asList("TBONTB", "OEROOE", " T"), solution1.printVertically("TO BE OR NOT TO BE"));
27+
}
28+
29+
@Test
30+
public void test3() {
31+
assertEquals(Arrays.asList("CIC", "OSO", "N M", "T I", "E N", "S G", "T"), solution1.printVertically("CONTEST IS COMING"));
32+
}
33+
34+
}

0 commit comments

Comments
 (0)