Created
October 29, 2019 17:20
-
-
Save andanf-e/2069113e566aa0769f4f22eb23738dcf 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 solution = (message, length) => { | |
const words = message.split(' '); | |
const filteredMessages = []; | |
let flag = true; | |
for (let word of words) { | |
if (word.length > length) { | |
flag = false; | |
break; | |
} | |
if ( | |
filteredMessages.length !== 0 && | |
filteredMessages[filteredMessages.length - 1].length + word.length < | |
length | |
) { | |
const newMessage = filteredMessages[filteredMessages.length - 1].concat( | |
' '.concat(word) | |
); | |
filteredMessages.splice(filteredMessages.length - 1, 1, newMessage); | |
} else { | |
filteredMessages.push(word); | |
} | |
} | |
return flag ? filteredMessages.length : -1; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment