原题链接在这里:https://leetcode.com/problems/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
题解:
The key is to find out the value of b.
To find out value of b, we can try from 0 and keep increamenting until it fulfills two conditions.
First, <b/b>, its length is getLen(b) * 2 + 3, its length must be < limit. Otherwise, there is no room for the submessage.
Second, overall length, sum of a's length + message length + (getLen(b + 3)) * b needs to be <= limit * b.
Keep trying until we find the b. Then add get the submessages one by one.
Time Complexity: O(n). n = message.length().
Space: O(n).
AC Java:
1 class Solution { 2 public String[] splitMessage(String message, int limit) { 3 int sumOfA = 0; 4 int b = 0; 5 int messageLen = message.length(); 6 while(getLen(b) * 2 + 3 < limit && sumOfA + messageLen + (getLen(b) + 3) * b > limit * b){ 7 b++; 8 sumOfA += getLen(b); 9 } 10 11 if(getLen(b) * 2 + 3 < limit){ 12 String[] res = new String[b]; 13 int pos = 0; 14 for(int i = 0; i < b; i++){ 15 StringBuilder sb = new StringBuilder(); 16 int subMessLen = limit - (getLen(b) + getLen(i + 1) + 3); 17 sb.append(message.substring(pos, Math.min(pos + subMessLen, messageLen))); 18 sb.append("<").append(i + 1).append("/").append(b).append(">"); 19 res[i] = sb.toString(); 20 pos += subMessLen; 21 } 22 return res; 23 } 24 25 return new String[0]; 26 } 27 28 private int getLen(int num){ 29 return String.valueOf(num).length(); 30 } 31 }
标签:Based,getLen,int,2468,length,parts,Limit,limit,message From: https://www.cnblogs.com/Dylan-Java-NYC/p/18118749