Skip to content

Commit cc6cb80

Browse files
committed
Text Justification/ Minimum Window Substring
1 parent a1ba8b2 commit cc6cb80

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

068 Text Justification.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'''
2+
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.
3+
4+
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 L characters.
5+
6+
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
7+
8+
For the last line of text, it should be left justified and no extra space is inserted between words.
9+
10+
For example,
11+
words: ["This", "is", "an", "example", "of", "text", "justification."]
12+
L: 16.
13+
14+
Return the formatted lines as:
15+
[
16+
"This is an",
17+
"example of text",
18+
"justification. "
19+
]
20+
Note: Each word is guaranteed not to exceed L in length.
21+
22+
A line other than the last line might contain only one word. What should you do in this case?
23+
In this case, that line should be left-justified.
24+
'''
25+
26+
class Solution(object):
27+
def fullJustify(self, words, maxWidth):
28+
"""
29+
:type words: List[str]
30+
:type maxWidth: int
31+
:rtype: List[str]
32+
"""
33+
start = end = 0
34+
result, curr_words_length = [], 0
35+
for i, word in enumerate(words):
36+
if len(word) + curr_words_length + end - start > maxWidth:
37+
if end - start == 1:
38+
result.append(words[start] + ' ' * (maxWidth - curr_words_length))
39+
else:
40+
total_space = maxWidth - curr_words_length
41+
space, extra = divmod(total_space, end - start - 1)
42+
for j in range(extra):
43+
words[start + j] += ' '
44+
result.append((' ' * space).join(words[start:end]))
45+
curr_words_length = 0
46+
start = end = i
47+
end += 1
48+
curr_words_length += len(word)
49+
result.append(' '.join(words[start:end]) + ' ' * (maxWidth - curr_words_length - (end - start - 1)))
50+
return result
51+
52+
53+
if __name__ == "__main__":
54+
assert Solution().fullJustify(["This", "is", "an", "example", "of", "text", "justification."], 16) == [
55+
"This is an",
56+
"example of text",
57+
"justification. "
58+
]

076 Minimum Window Substring.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
3+
4+
For example,
5+
S = "ADOBECODEBANC"
6+
T = "ABC"
7+
Minimum window is "BANC".
8+
9+
Note:
10+
If there is no such window in S that covers all characters in T, return the empty string "".
11+
12+
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
13+
'''
14+
15+
from collections import defaultdict
16+
17+
18+
class Solution(object):
19+
def minWindow(self, s, t):
20+
"""
21+
:type s: str
22+
:type t: str
23+
:rtype: str
24+
"""
25+
MAX_INT = 2147483647
26+
start = end = 0
27+
char_need = defaultdict(int) # the count of char needed by current window, negative means current window has it but not needs it
28+
count_need = len(t) # count of chars not in current window but in t
29+
min_length = MAX_INT
30+
min_start = 0
31+
for i in t:
32+
char_need[i] += 1 # current window needs all char in t
33+
while end < len(s):
34+
if char_need[s[end]] > 0:
35+
count_need -= 1
36+
char_need[s[end]] -= 1 # current window contains s[end] now, so does not need it any more
37+
end += 1
38+
while count_need == 0:
39+
if min_length > end - start:
40+
min_length = end - start
41+
min_start = start
42+
char_need[s[start]] += 1 # current window does not contain s[start] any more
43+
if char_need[s[start]] > 0: # when some count in char_need is positive, it means there is char in t but not current window
44+
count_need += 1
45+
start += 1
46+
return "" if min_length == MAX_INT else s[min_start:min_start + min_length]
47+
48+
49+
if __name__ == "__main__":
50+
assert Solution().minWindow("ADOBECODEBANC", "ABC") == "BANC"

0 commit comments

Comments
 (0)