java.lang.Object
g2401_2500.s2468_split_message_based_on_limit.Solution

public class Solution extends Object
2468 - Split Message Based on Limit.<p>Hard</p> <p>You are given a string, <code>message</code>, and a positive integer, <code>limit</code>.</p> <p>You must <strong>split</strong> <code>message</code> into one or more <strong>parts</strong> based on <code>limit</code>. Each resulting part should have the suffix <code>&quot;<a/b>&quot;</code>, where <code>&quot;b&quot;</code> is to be <strong>replaced</strong> with the total number of parts and <code>&quot;a&quot;</code> is to be <strong>replaced</strong> with the index of the part, starting from <code>1</code> and going up to <code>b</code>. Additionally, the length of each resulting part (including its suffix) should be <strong>equal</strong> to <code>limit</code>, except for the last part whose length can be <strong>at most</strong> <code>limit</code>.</p> <p>The resulting parts should be formed such that when their suffixes are removed and they are all concatenated <strong>in order</strong> , they should be equal to <code>message</code>. Also, the result should contain as few parts as possible.</p> <p>Return <em>the parts</em> <code>message</code> <em>would be split into as an array of strings</em>. If it is impossible to split <code>message</code> as required, return <em>an empty array</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> message = &ldquo;this is really a very awesome message&rdquo;, limit = 9</p> <p><strong>Output:</strong> [&ldquo;thi<1/14>&rdquo;,&ldquo;s i<2/14>&rdquo;,&ldquo;s r<3/14>&rdquo;,&ldquo;eal<4/14>&rdquo;,&ldquo;ly <5/14>&rdquo;,&ldquo;a v<6/14>&rdquo;,&ldquo;ery<7/14>&rdquo;,&quot; aw<8/14>&ldquo;,&ldquo;eso<9/14>&rdquo;,&ldquo;me<10/14>&rdquo;,&rdquo; m<11/14>&quot;,&ldquo;es<12/14>&rdquo;,&ldquo;sa<13/14>&rdquo;,&ldquo;ge<14/14>&rdquo;]</p> <p><strong>Explanation:</strong></p> <p>The first 9 parts take 3 characters each from the beginning of message.</p> <p>The next 5 parts take 2 characters each to finish splitting message.</p> <p>In this example, each part, including the last, has length 9.</p> <p>It can be shown it is not possible to split message into less than 14 parts.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> message = &ldquo;short message&rdquo;, limit = 15</p> <p><strong>Output:</strong> [&ldquo;short mess<1/2>&rdquo;,&ldquo;age<2/2>&rdquo;]</p> <p><strong>Explanation:</strong></p> <p>Under the given constraints, the string can be split into two parts:</p> <ul> <li> <p>The first part comprises of the first 10 characters, and has a length 15.</p> </li> <li> <p>The next part comprises of the last 3 characters, and has a length 8.</p> </li> </ul> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= message.length <= 10<sup>4</sup></code></li> <li><code>message</code> consists only of lowercase English letters and <code>' '</code>.</li> <li><code>1 <= limit <= 10<sup>4</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • splitMessage

      public String[] splitMessage(String message, int limit)