返回介绍

solution / 2400-2499 / 2468.Split Message Based on Limit / README_EN

发布于 2024-06-17 01:03:05 字数 7439 浏览 0 评论 0 收藏 0

2468. Split Message Based on Limit

中文文档

Description

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:

Input: message = "this is really a very awesome message", limit = 9
Output: ["thi<1/14>","s i<2/14>","s r<3/14>","eal<4/14>","ly <5/14>","a v<6/14>","ery<7/14>"," aw<8/14>","eso<9/14>","me<10/14>"," m<11/14>","es<12/14>","sa<13/14>","ge<14/14>"]
Explanation:
The first 9 parts take 3 characters each from the beginning of message.
The next 5 parts take 2 characters each to finish splitting message. 
In this example, each part, including the last, has length 9. 
It can be shown it is not possible to split message into less than 14 parts.

Example 2:

Input: message = "short message", limit = 15
Output: ["short mess<1/2>","age<2/2>"]
Explanation:
Under the given constraints, the string can be split into two parts: 
- The first part comprises of the first 10 characters, and has a length 15.
- The next part comprises of the last 3 characters, and has a length 8.

 

Constraints:

  • 1 <= message.length <= 104
  • message consists only of lowercase English letters and ' '.
  • 1 <= limit <= 104

Solutions

Solution 1: Enumerate the Number of Segments + Simulation

We denote the length of the string message as $n$, and the number of segments as $k$.

According to the problem, if $k > n$, it means that we can divide the string into more than $n$ segments. Since the length of the string is only $n$, dividing it into more than $n$ segments will inevitably lead to some segments with a length of $0$, which can be deleted. Therefore, we only need to limit the range of $k$ to $[1,.. n]$.

We enumerate the number of segments $k$ from small to large. Let the length of $a$ segments in all segments be $sa$, the length of $b$ segments in all segments be $sb$, and the length of all symbols (including angle brackets and slashes) in all segments be $sc$.

Then the value of $sa$ is ${\textstyle \sum_{j=1}^{k}} len(s_j)$, which can be directly obtained through the prefix sum; the value of $sb$ is $len(str(k)) \times k$; and the value of $sc$ is $3 \times k$.

Therefore, the number of characters that can be filled in all segments is $limit\times k - (sa + sb + sc)$. If this value is greater than or equal to $n$, it means that the string can be divided into $k$ segments, and we can directly construct the answer and return it.

The time complexity is $O(n\times \log n)$, where $n$ is the length of the string message. Ignoring the space consumption of the answer, the space complexity is $O(1)$.

class Solution:
  def splitMessage(self, message: str, limit: int) -> List[str]:
    n = len(message)
    sa = 0
    for k in range(1, n + 1):
      sa += len(str(k))
      sb = len(str(k)) * k
      sc = 3 * k
      if limit * k - (sa + sb + sc) >= n:
        ans = []
        i = 0
        for j in range(1, k + 1):
          tail = f'<{j}/{k}>'
          t = message[i : i + limit - len(tail)] + tail
          ans.append(t)
          i += limit - len(tail)
        return ans
    return []
class Solution {
  public String[] splitMessage(String message, int limit) {
    int n = message.length();
    int sa = 0;
    String[] ans = new String[0];
    for (int k = 1; k <= n; ++k) {
      int lk = (k + "").length();
      sa += lk;
      int sb = lk * k;
      int sc = 3 * k;
      if (limit * k - (sa + sb + sc) >= n) {
        int i = 0;
        ans = new String[k];
        for (int j = 1; j <= k; ++j) {
          String tail = String.format("<%d/%d>", j, k);
          String t = message.substring(i, Math.min(n, i + limit - tail.length())) + tail;
          ans[j - 1] = t;
          i += limit - tail.length();
        }
        break;
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<string> splitMessage(string message, int limit) {
    int n = message.size();
    int sa = 0;
    vector<string> ans;
    for (int k = 1; k <= n; ++k) {
      int lk = to_string(k).size();
      sa += lk;
      int sb = lk * k;
      int sc = 3 * k;
      if (k * limit - (sa + sb + sc) >= n) {
        int i = 0;
        for (int j = 1; j <= k; ++j) {
          string tail = "<" + to_string(j) + "/" + to_string(k) + ">";
          string t = message.substr(i, limit - tail.size()) + tail;
          ans.emplace_back(t);
          i += limit - tail.size();
        }
        break;
      }
    }
    return ans;
  }
};
func splitMessage(message string, limit int) (ans []string) {
  n := len(message)
  sa := 0
  for k := 1; k <= n; k++ {
    lk := len(strconv.Itoa(k))
    sa += lk
    sb := lk * k
    sc := 3 * k
    if limit*k-(sa+sb+sc) >= n {
      i := 0
      for j := 1; j <= k; j++ {
        tail := "<" + strconv.Itoa(j) + "/" + strconv.Itoa(k) + ">"
        t := message[i:min(i+limit-len(tail), n)] + tail
        ans = append(ans, t)
        i += limit - len(tail)
      }
      break
    }
  }
  return
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文