You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an array of strings `words` and a width `maxWidth`, format the text such that each line has exactly `maxWidth` characters and is fully (left and right) justified.
6
+
7
+
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces `' '` when necessary so that each line has exactly `maxWidth` characters.
8
+
9
+
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
10
+
11
+
For the last line of text, it should be left-justified and no extra space is inserted between words.
12
+
13
+
**Note:**
14
+
15
+
* A word is defined as a character sequence consisting of non-space characters only.
16
+
* Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
17
+
* The input array `words` contains at least one word.
**Output:**[ "This is an", "example of text", "justification. " ]
24
+
25
+
**Example 2:**
26
+
27
+
**Input:** words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
28
+
29
+
**Output:**[ "What must be", "acknowledgment ", "shall be " ]
30
+
31
+
**Explanation:** Note that the last line is "shall be " instead of "shall be", because the last line must be left-justified instead of fully-justified. Note that the second line is also left-justified becase it contains only one word.
32
+
33
+
**Example 3:**
34
+
35
+
**Input:** words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20
36
+
37
+
**Output:**[ "Science is what we", "understand well", "enough to explain to", "a computer. Art is", "everything else we", "do " ]
38
+
39
+
**Constraints:**
40
+
41
+
*`1 <= words.length <= 300`
42
+
*`1 <= words[i].length <= 20`
43
+
*`words[i]` consists of only English letters and symbols.
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**.
6
+
7
+
In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level, and any multiple consecutive slashes (i.e. `'//'`) are treated as a single slash `'/'`. For this problem, any other format of periods such as `'...'` are treated as file/directory names.
8
+
9
+
The **canonical path** should have the following format:
10
+
11
+
* The path starts with a single slash `'/'`.
12
+
* Any two directories are separated by a single slash `'/'`.
13
+
* The path does not end with a trailing `'/'`.
14
+
* The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period `'.'` or double period `'..'`)
15
+
16
+
Return _the simplified **canonical path**_.
17
+
18
+
**Example 1:**
19
+
20
+
**Input:** path = "/home/"
21
+
22
+
**Output:** "/home"
23
+
24
+
**Explanation:** Note that there is no trailing slash after the last directory name.
25
+
26
+
**Example 2:**
27
+
28
+
**Input:** path = "/../"
29
+
30
+
**Output:** "/"
31
+
32
+
**Explanation:** Going one level up from the root directory is a no-op, as the root level is the highest level you can go.
33
+
34
+
**Example 3:**
35
+
36
+
**Input:** path = "/home//foo/"
37
+
38
+
**Output:** "/home/foo"
39
+
40
+
**Explanation:** In the canonical path, multiple consecutive slashes are replaced by a single one.
41
+
42
+
**Example 4:**
43
+
44
+
**Input:** path = "/a/./b/../../c/"
45
+
46
+
**Output:** "/c"
47
+
48
+
**Constraints:**
49
+
50
+
*`1 <= path.length <= 3000`
51
+
*`path` consists of English letters, digits, period `'.'`, slash `'/'` or `'_'`.
0 commit comments