Skip to content

Instantly share code, notes, and snippets.

@jovianlin
Created January 14, 2019 04:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jovianlin/f61b39a8d57f0c8a417568024743ce41 to your computer and use it in GitHub Desktop.
Save jovianlin/f61b39a8d57f0c8a417568024743ce41 to your computer and use it in GitHub Desktop.
Daily Coding Problem: Problem #57
"""
Good morning! Here's your coding interview problem for today.
This problem was asked by Amazon.
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.
"""
def solve(s, k):
arr = s.split()
output = []
buffer_arr = []
buffer_len = -1
i = 0
while i < len(arr):
w = arr[i]
if len(w) > k:
print('\n[*] Cannot complete because of this word: "%s"' % w)
return
if (len(w) + buffer_len + 1) <= k:
buffer_len += len(w) + 1
buffer_arr.append(w)
i += 1
else:
result = ' '.join(buffer_arr)
output.append(result)
buffer_arr = []
buffer_len = -1
# End of while loop.
if buffer_arr: # If there're still items in the buffer, output them.
result = ' '.join(buffer_arr)
output.append(result)
return output
# End of solve(...)
input_str = "THESE TERMS AND CONDITIONS OF SERVICE (the Terms) ARE A LEGAL AND BINDING AGREEMENT BETWEEN YOU AND NATIONAL GEOGRAPHIC governing your use of this site, www.nationalgeographic.com, which includes but is not limited to products, software and services offered by way of the website such as the Video Player, Uploader, and other applications that link to these Terms (the Site). Please review the Terms fully before you continue to use the Site. By using the Site, you agree to be bound by the Terms. You shall also be subject to any additional terms posted with respect to individual sections of the Site. Please review our Privacy Policy, which also governs your use of the Site, to understand our practices. If you do not agree, please discontinue using the Site. National Geographic reserves the right to change the Terms at any time without prior notice. Your continued access or use of the Site after such changes indicates your acceptance of the Terms as modified. It is your responsibility to review the Terms regularly. The Terms were last updated on 18 July 2011."
X = solve(input_str, k=80)
for s in X:
print(s)
"""
Output:
THESE TERMS AND CONDITIONS OF SERVICE (the Terms) ARE A LEGAL AND BINDING
AGREEMENT BETWEEN YOU AND NATIONAL GEOGRAPHIC governing your use of this site,
www.nationalgeographic.com, which includes but is not limited to products,
software and services offered by way of the website such as the Video Player,
Uploader, and other applications that link to these Terms (the Site). Please
review the Terms fully before you continue to use the Site. By using the Site,
you agree to be bound by the Terms. You shall also be subject to any additional
terms posted with respect to individual sections of the Site. Please review our
Privacy Policy, which also governs your use of the Site, to understand our
practices. If you do not agree, please discontinue using the Site. National
Geographic reserves the right to change the Terms at any time without prior
notice. Your continued access or use of the Site after such changes indicates
your acceptance of the Terms as modified. It is your responsibility to review
the Terms regularly. The Terms were last updated on 18 July 2011.
"""
@Enbidi
Copy link

Enbidi commented Apr 24, 2021

Hey man, I have a shorter solution than yours and don't need extra space:
def solve(s, k):
s = s.strip()
res = []
curLength = 0
start = 0
t = 0
l = len(s)
for i in range(l):
curLength += 1
if curLength > k:
res.append(s[start:t])
start = t + 1
curLength = i - t - 1
if s[i] == ' ':
t = i
if start < l:
res.append(s[start:l])

return res

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment