-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
40 lines (37 loc) · 961 Bytes
/
Copy pathSolution.java
File metadata and controls
40 lines (37 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.ArrayList;
import java.util.List;
class Solution
{
public List<String> printVertically(String s)
{
String[] words = s.split(" ");
int maxLen = 0;
for (String word : words)
{
maxLen = Math.max(maxLen, word.length());
}
final List<String> result = new ArrayList<>();
for (int i = 0; i < maxLen; i++)
{
StringBuilder sb = new StringBuilder();
for (String word : words)
{
if (i >= word.length())
{
sb.append(" ");
}
else
{
sb.append(word.charAt(i));
}
}
int j = words.length - 1;
while (sb.charAt(j) == ' ')
{
sb.deleteCharAt(j--);
}
result.add(sb.toString());
}
return result;
}
}