Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Kickstart testing example

You have a long piece of text, and you want to send it to your friend as a series of SMS messages. Your text consists of English letters (uppercase and lowercase) and spaces. The text doesn't contain any leading spaces at the beginning or trailing spaces at the end. There are no two consecutive spaces in the text.

One proper SMS can contain at most K characters. If the text is longer, it needs to be split into parts. Each part should be sent in a separate message that fulfills the maximum length requirement. Text may not be split within words. The order of the words and the messages can't change, so that we can later concatenate the resulting messages to retrieve the original text. Spaces at the start and end of all messages are removed, and thus are not accounted for in the overall message length.

The goal is to count the number of SMS messages needed to accommodate the whole text, keeping the length of each message less than or equal to K. The total number of SMS messages must be kept to a minimum, however; it would not be acceptable, for example, to take the wasteful approach of programmatically sending one SMS for each word in the original text!

Write a function: function solution(S, K);

that, given a string S representing the text that needs to be split and an integer K that is equal to the maximum possible message length, returns the number of SMS messages needed.

For instance, given string S="SMS messages are really short" and K = 12, your function should return 3. You could split this text into proper messages as follows:

"SMS messages" "are really" "short"

If it's impossible to split text into proper SMS messages, your function should return −1.

Assume that:

  • K is an integer within the range [1..500];
  • S is a non-empty string containing at most 500 characters: only letters and spaces, there are no spaces at the beginning and at the end of S; also there can't be two or more consecutive spaces in S. In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
@flushdev

This comment has been minimized.

Copy link

@flushdev flushdev commented Oct 10, 2019

@Said-FD

This comment has been minimized.

Copy link

@Said-FD Said-FD commented Oct 11, 2019

@PAtrofimov

This comment has been minimized.

@kravtsivkostya

This comment has been minimized.

@fa4fun

This comment has been minimized.

@murarova

This comment has been minimized.

@vitket

This comment has been minimized.

Copy link

@vitket vitket commented Oct 27, 2019

solution (Swift)

@R-A-S

This comment has been minimized.

Copy link

@R-A-S R-A-S commented Oct 28, 2019

@LenaAGM

This comment has been minimized.

@s1oux

This comment has been minimized.

Copy link

@s1oux s1oux commented Oct 29, 2019

@tanya-melnyk

This comment has been minimized.

Copy link

@tanya-melnyk tanya-melnyk commented Oct 30, 2019

@dimsdale

This comment has been minimized.

Copy link

@dimsdale dimsdale commented Nov 1, 2019

@pavliukdmytro

This comment has been minimized.

Copy link

@pavliukdmytro pavliukdmytro commented Nov 5, 2019

@PanArtemG

This comment has been minimized.

@kulia26

This comment has been minimized.

Copy link

@kulia26 kulia26 commented Nov 8, 2019

@Podoima

This comment has been minimized.

Copy link

@Podoima Podoima commented Nov 10, 2019

@vlasenkorm

This comment has been minimized.

@olena-olena

This comment has been minimized.

Copy link

@olena-olena olena-olena commented Nov 14, 2019

@siniiitsa

This comment has been minimized.

Copy link

@siniiitsa siniiitsa commented Nov 23, 2019

@helsworkflow

This comment has been minimized.

@quickEscape

This comment has been minimized.

Copy link

@quickEscape quickEscape commented Nov 27, 2019

@Maryna2019

This comment has been minimized.

Copy link

@Maryna2019 Maryna2019 commented Nov 28, 2019

@503292

This comment has been minimized.

@JJpackman

This comment has been minimized.

@YanaGushlevskaya

This comment has been minimized.

Copy link

@YanaGushlevskaya YanaGushlevskaya commented Dec 1, 2019

@n1cko22

This comment has been minimized.

@ckmirafss

This comment has been minimized.

Copy link

@ckmirafss ckmirafss commented Apr 2, 2020

PHP VERSION ANYOONE>?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.