Smart Tweet Splitter

Ninja
0/200
0 upvote
Asked in company
Wednesday Solutions

Problem statement

You are given a long string of text and a maximum character length for a tweet. Your task is to write a function that splits the text into one or more tweets.

The splitting must adhere to the following rules:

  • Each tweet string must have a length less than or equal to maxLength.


  • Splitting must only occur at word boundaries (i.e., on spaces). A word must not be broken in the middle.


  • If the text is split into multiple tweets, a numbering suffix like (1/3), (2/3), (3/3) must be appended to each tweet.


  • The length of this numbering suffix must be included when calculating the total character count of a tweet.


    Your function should return a list or array of the resulting tweet strings.


  • Detailed explanation ( Input/output format, Notes, Images )
    Input Format:
    The first line of input contains the string text.
    The second line of input contains an integer maxLength.
    


    Output Format:
    Print the resulting tweet strings, one per line.
    If the text is empty or contains only spaces, print nothing.
    If any single word in the text is too long to fit in a tweet even by itself (after accounting for the suffix), the task is impossible. In this case, print a single line with the string Error: Word too long.
    


    Note:
    The core challenge is that you don't know the total number of tweets (T in (i/T)) until you've already split the text. But the length of T (e.g., whether it's a 1-digit or 2-digit number) affects the available space in each tweet, which in turn affects the splitting.
    The optimal solution involves an iterative approach: first, try to split the text assuming there will be 1 total tweet. If that fails, try to split it assuming there will be 2 total tweets, and so on, until a valid split is found. The first valid split will be the one with the minimum possible number of tweets.
    
    Sample Input 1:
    This is a very long text that needs to be split into several parts because it exceeds the character limit.
    50
    


    Sample Output 1:
    This is a very long text that needs to be (1/3)
    split into several parts because it exceeds (2/3)
    the character limit. (3/3)
    


    Explanation for Sample 1:
    The function determines that the text must be split into 3 tweets. It calculates the space needed for the suffix `(x/3)` (which is 4 characters) and then greedily fits words into each tweet.
    - Tweet 1: `45 chars of text + 1 space + 4 char suffix = 50`.
    - Tweet 2: `42 chars of text + 1 space + 4 char suffix = 47`.
    - Tweet 3: `22 chars of text + 1 space + 4 char suffix = 27`.
    All are ≤ 50 characters.
    


    Sample Input 2:
    antidisestablishmentarianism is a long word
    25
    


    Sample Output 2:
    Error: Word too long
    


    Explanation for Sample 2:
    The word "antidisestablishmentarianism" has 28 characters. This is longer than the `maxLength` of 25, so it's impossible to create a valid tweet.
    


    Expected Time Complexity:
    The expected time complexity is O(N^2), where N is the number of words in the string.
    


    Constraints:
    1 <= N (length of text) <= 5000
    10 <= maxLength <= 280
    
    Time limit: 1 sec
    
    Approaches (1)
    Smart Tweet Splitter
    Time Complexity

    The expected time complexity is O(N^2), where N is the number of words in the string.

    Space Complexity
    Code Solution
    (100% EXP penalty)
    Smart Tweet Splitter
    Full screen
    Console