FastPrepPrepare Notification πŸ“¬

Prepare Notification πŸ“¬

General Motors logoGeneral Motors● EasyFULLTIMEOA

Problem statement

Prepare a notification of the given message which will be displayed on a mobile device. The message is made of words separated by single spaces. The length of the notification is limited to K characters. If the message is too long to be displayed fully, some words from the end of the message should be cropped, keeping in mind that:

  • the notification should be as long as possible;
  • only whole words can be cropped;
  • if any words are cropped, the notification should end with '...'; the dots should be separated from the last word by a single space;
  • the notification may not exceed the K-character limit, including the dots.

If all the words need to be cropped, the notification is '...' (three dots without a space).

Write up the func in the editor πŸ‘‰

Given a string message and an integer K, returns the notification to display, which has no more than K chars, as described above.

Function

prepareNotification(message: String, K: int) β†’ String

Examples

Example 1

message = "And now here is my secret"K = 15return = "And now ..."

no explanation is provided πŸ₯Ή

Example 2

message = "There is an animal with four legs"K = 15return = "There is an ..."

no explanation is provided

Example 3

message = "super dog"K = 4return = "..."

no explanation is provided

Example 4

message = "how are you"K = 20return = "how are you"

no explanation is provided

Constraints

  • K is an integer within the range [3, 500]
  • the len of string msg i within the range [1, 500]
  • string msg is made of English letters ('a' - 'z', 'A-Z') and space ('')
  • msg does not contains spaces at the beginning of at the end
  • words are separated by a single space (there are never 2 or more consecutive sapces)

More General Motors problems

See General Motors hiring insights
Saved
public String prepareNotification(String message, int K) {
    // write your code here
}
message"And now here is my secret"
K15
expected"And now ..."
Checking account…