Skip to content
Rain Hu's Workspace
Go back

[LeetCode] 2468. Split Message Based on Limit

Rain Hu

2468. Split Message Based on Limit


一、題目

You are given a string, message, and a positive integer, limit.

You must split message into one or more parts based on limit. Each resulting part should have the suffix "<a/b>", where "b" is to be replaced with the total number of parts and "a" is to be replaced with the index of the part, starting from 1 and going up to b. Additionally, the length of each resulting part (including its suffix) should be equal to limit, except for the last part whose length can be at most limit.

The resulting parts should be formed such that when their suffixes are removed and they are all concatenated in order, they should be equal to message. Also, the result should contain as few parts as possible.

Return the parts message would be split into as an array of strings. If it is impossible to split message as required, return an empty array.

Example 1:

Example 2:

Constraints:


二、分析

三、解題

1. Math

    vector<string> splitMessage(string message, int limit) {
    if (limit <= 5) return {};
    int len = message.length();
    int left = 1;
    for ( ;left < len; left++) {
        if (limit * left >= f(left) + len) break;
    }
    if (limit * left < f(left) + len) return {};
    vector<string> res(left);
    int strlen = to_string(left).length() + 3;
    int acc = 0;
    for (int i = 1; i <= left; i++) {
        len = strlen + to_string(i).length();
        res[i-1] = message.substr(acc, limit-len) + "<" + to_string(i) + "/" + to_string(left) + ">";
        acc += limit-len;
    }
    return res;
}
int f(int n) {
    if (n < 10) {
        return n*5;
    } else if (n < 100) {
        return n*7-9;
    } else if (n < 1000) {
        return n*9-108;
    } else if (n < 10000) {
        return n*11-1107;
    }
    return n*13-11106;
}
vector<string> splitMessage(string message, int limit) {
    int b = 0 , cnt = 0 , sm = 0;
    vector<string> ans;
    for(int i=1; i<=10000; i++) {
        sm+=Size(i);  // sum of length of ('1') + ('2')... ('i')  , we are calculating sum of length of all a's.
        int cnt = ((3 + Size(i)) * i) + message.size() + sm;   // sum of (3 is "</>"  + i's size ) * i times , message , sm 
        int len = (i-1) * limit; // till second last
        if(cnt - len <= limit) {  // if last is bigger than limit , its invalid!
            b = i;
            break;
        }
    }
    string s = "";
    cnt = 1;
    for(int i=0; i<message.size(); i++) {
            if(limit - (3 + Size(cnt) + Size(b) + (int)s.size())>0) {
                s+=message[i];
            }else {
                string word = s + "<" + to_string(cnt) + "/" + to_string(b) + ">";
                ans.push_back(word);
                s = message[i];
                cnt++;
            }
    }
        string word = s + "<" + to_string(cnt) + "/" + to_string(b) + ">";
        ans.push_back(word);
        if(cnt>b || word.size()>limit) return {};   // cnt is last value of a which should never be > than b , also last word size should be <= limit!
    return ans;
}
int Size(int n) {
    return to_string(n).size();
}

回目錄 Catalog


Share this post on:

Previous
[LeetCode] 300. Longest Increasing Subsequence
Next
[LeetCode] 2467. Most Profitable Path in a Tree