Skip to content

2468. Split Message Based on Limit 👎

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
 public:
  vector<string> splitMessage(string message, int limit) {
    const int kMessageLength = message.length();
    int b = 1;
    // the total length of a: initialized with the length of "1"
    int aLength = sz(1);

    // the total length of b := b * sz(b)
    // The total length of "</>" := b * 3
    while (b * limit < b * (sz(b) + 3) + aLength + kMessageLength) {
      // If the length of the last suffix "<b/b>" := sz(b) * 2 + 3 >= limit,
      // then it's impossible that the length of "*<b/b>" <= limit.
      if (sz(b) * 2 + 3 >= limit)
        return {};
      aLength += sz(++b);
    }

    vector<string> ans;

    for (int i = 0, a = 1; a <= b; ++a) {
      // the length of "<a/b>" := sz(a) + sz(b) + 3
      const int j = limit - (sz(a) + sz(b) + 3);
      ans.push_back(message.substr(i, j) + "<" + to_string(a) + "/" +
                    to_string(b) + ">");
      i += j;
    }

    return ans;
  }

 private:
  int sz(int num) {
    return to_string(num).length();
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
  public String[] splitMessage(String message, int limit) {
    final int kMessageLength = message.length();
    int b = 1;
    // the total length of a: initialized with the length of "1"
    int aLength = sz(1);

    // the total length of b := b * sz(b)
    // The total length of "</>" := b * 3
    while (b * limit < b * (sz(b) + 3) + aLength + kMessageLength) {
      // If the length of the last suffix "<b/b>" := sz(b) * 2 + 3 >= limit,
      // then it's impossible that the length of "*<b/b>" <= limit.
      if (sz(b) * 2 + 3 >= limit)
        return new String[] {};
      aLength += sz(++b);
    }

    String[] ans = new String[b];

    for (int i = 0, a = 1; a <= b; ++a) {
      // the length of "<a/b>" := sz(a) + sz(b) + 3
      final int j = limit - (sz(a) + sz(b) + 3);
      ans[a - 1] = message.substring(i, Math.min(message.length(), i + j)) + "<" +
                   String.valueOf(a) + "/" + String.valueOf(b) + ">";
      i += j;
    }

    return ans;
  }

  private int sz(int num) {
    return String.valueOf(num).length();
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution:
  def splitMessage(self, message: str, limit: int) -> list[str]:
    kMessageLength = len(message)

    def sz(num: int):
      return len(str(num))

    b = 1
    # the total length of a: initialized with the length of "1"
    aLength = sz(1)

    # the total length of b := b * sz(b)
    # The total length of "</>" := b * 3
    while b * limit < b * (sz(b) + 3) + aLength + kMessageLength:
      # If the length of the last suffix "<b/b>" := sz(b) * 2 + 3 >= limit,
      # then it's impossible that the length of "*<b/b>" <= limit.
      if sz(b) * 2 + 3 >= limit:
        return []
      b += 1
      aLength += sz(b)

    ans = []

    i = 0
    for a in range(1, b + 1):
      # the length of "<a/b>" := sz(a) + sz(b) + 3
      j = limit - (sz(a) + sz(b) + 3)
      ans.append(f'{message[i:i + j]}<{a}/{b}>')
      i += j

    return ans