Skip to content

Instantly share code, notes, and snippets.

@tanya-melnyk
Created October 30, 2019 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanya-melnyk/e78a4d8cec4245d0463b5dbe8c24e0a9 to your computer and use it in GitHub Desktop.
Save tanya-melnyk/e78a4d8cec4245d0463b5dbe8c24e0a9 to your computer and use it in GitHub Desktop.
const S = "SMS messages are really short";
const K = 12;
function solution(S, K) {
const arr = S.split(" ").map(word => word.length);
let messagesCount = 1;
let message = arr[0];
for (let i = 1; i < arr.length; i += 1) {
if (arr[0] > K || arr[i] > K) return -1;
if (message + 1 + arr[i] <= K) {
message += 1 + arr[i];
} else {
messagesCount += 1;
message = arr[i];
}
}
return messagesCount;
}
solution(S, K);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment