-
Notifications
You must be signed in to change notification settings - Fork 0
/
057.py
57 lines (46 loc) · 1.73 KB
/
057.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Problem:
Given a string s and an integer k, break up the string into multiple texts such that
each text has a length of k or less. You must break it up so that words don't break
across lines. If there's no way to break the text up, then return null.
You can assume that there are no spaces at the ends of the string and that there is
exactly one space between each word.
For example, given the string "the quick brown fox jumps over the lazy dog" and k = 10,
you should return: ["the quick", "brown fox", "jumps over", "the lazy", "dog"]. No
string in the list has a length of more than 10.
"""
from typing import List, Optional
def break_string(string: str, k: int) -> Optional[List[str]]:
word_list = string.split()
result = []
curr_len = 0
curr_str = ""
# generating the formatted text
for word in word_list:
current_word_length = len(word)
if current_word_length > k:
return None
if curr_len == 0:
# first iteration
curr_len = current_word_length
curr_str = word
elif curr_len + current_word_length + 1 > k:
# overflow condition
result.append(curr_str)
curr_str = word
curr_len = current_word_length
else:
# normal addition to the string
curr_len += current_word_length
curr_str += " " + word
result.append(curr_str)
return result
if __name__ == "__main__":
print(break_string("the quick brown fox jumps over the lazy dog", 10))
print(break_string("the quick brown fox jumps over the lazy dog", 3))
print(break_string("the quick brown fox jumps over the lazy dog tenletters", 10))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""