Created
October 30, 2019 06:09
-
-
Save tanya-melnyk/e78a4d8cec4245d0463b5dbe8c24e0a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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