Description
Solutions
Submission
Prepare Notification
🔥 FULLTIME

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).

    Functin Description:

    Complete function prepareNotification in the editor.

    Function prepareNotification has the following parameters:

  • String message & int k
  • Returns:

  • The notification to display, which has no more than K chars, as described above.
  • Example 1:

    Input:  message = "And now here is my secret", K = 15
    Output: "And now..."
    Explanation:
  • the notification "And ..." would be incorrect, because there is a longer correct notification
  • the notification "And now her ..." would be incorrect, because the original message is cropped through the middle of a word
  • the notification "And now ..." would be incorrect, because it ends with a space
  • the notification "And now here..." would be incorrect, because it ends with a space
  • the notification "And now here..." would be incorrect, because there is no space before the three dots
  • the notification "And now here ..." would be incorrect, because it exceeds the 15-character limit
  • The function should return "And now...", as explained above.

    Example 2:

    Input:  message = "There is an animal with four legs", K = 15
    Output: "There is an ..."
    Explanation:
    The function should return "There is an...".

    Example 3:

    Input:  message = "super dog", K = 4
    Output: "..."
    Explanation:
    The function should return "...".

    Example 4:

    Input:  message = "how are you", K = 20
    Output: "how are you"
    Explanation:
    The function should return "how are you".
    Constraints:
    • K is an integer within the range [3..500]
    • the length of string message is within the range [1..500]
    • string message is made of English letters (a-z, 'A-Z) and spaces
    • message does not contain spaces at the beginning or at the end
    • words are separated by a single space (there are never two or more consecutive spaces)
    Thumbnail 0
    Testcase

    Result
    Case 1

    input:

    output: