FastPrepFastPrep
Problem Brief

Prepare Notification πŸ“¬

FULLTIMEOA

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.

    1Example 1

    Input
    message = "And now here is my secret", K = 15
    Output
    "And now ..."
    Explanation
    no explanation is provided πŸ₯Ή

    2Example 2

    Input
    message = "There is an animal with four legs", K = 15
    Output
    "There is an ..."
    Explanation
    no explanation is provided

    3Example 3

    Input
    message = "super dog", K = 4
    Output
    "..."
    Explanation
    no explanation is provided

    4Example 4

    Input
    message = "how are you", K = 20
    Output
    "how are you"
    Explanation
    no explanation is provided

    Constraints

    Limits and guarantees your solution can rely on.

  • 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)
  • public String prepareNotification(String message, int K) {
        // write your code here
    }
    
    Input

    message

    "And now here is my secret"

    K

    15

    Output

    "And now ..."

    Sign in to submit your solution.