java.lang.Object
g2101_2200.s2138_divide_a_string_into_groups_of_size_k.Solution

public class Solution extends Object
2138 - Divide a String Into Groups of Size k.<p>Easy</p> <p>A string <code>s</code> can be partitioned into groups of size <code>k</code> using the following procedure:</p> <ul> <li>The first group consists of the first <code>k</code> characters of the string, the second group consists of the next <code>k</code> characters of the string, and so on. Each character can be a part of <strong>exactly one</strong> group.</li> <li>For the last group, if the string <strong>does not</strong> have <code>k</code> characters remaining, a character <code>fill</code> is used to complete the group.</li> </ul> <p>Note that the partition is done so that after removing the <code>fill</code> character from the last group (if it exists) and concatenating all the groups in order, the resultant string should be <code>s</code>.</p> <p>Given the string <code>s</code>, the size of each group <code>k</code> and the character <code>fill</code>, return <em>a string array denoting the <strong>composition of every group</strong></em> <code>s</code> <em>has been divided into, using the above procedure</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;abcdefghi&rdquo;, k = 3, fill = &ldquo;x&rdquo;</p> <p><strong>Output:</strong> [&ldquo;abc&rdquo;,&ldquo;def&rdquo;,&ldquo;ghi&rdquo;]</p> <p><strong>Explanation:</strong></p> <p>The first 3 characters &ldquo;abc&rdquo; form the first group.</p> <p>The next 3 characters &ldquo;def&rdquo; form the second group.</p> <p>The last 3 characters &ldquo;ghi&rdquo; form the third group.</p> <p>Since all groups can be completely filled by characters from the string, we do not need to use fill.</p> <p>Thus, the groups formed are &ldquo;abc&rdquo;, &ldquo;def&rdquo;, and &ldquo;ghi&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;abcdefghij&rdquo;, k = 3, fill = &ldquo;x&rdquo;</p> <p><strong>Output:</strong> [&ldquo;abc&rdquo;,&ldquo;def&rdquo;,&ldquo;ghi&rdquo;,&ldquo;jxx&rdquo;]</p> <p><strong>Explanation:</strong></p> <p>Similar to the previous example, we are forming the first three groups &ldquo;abc&rdquo;, &ldquo;def&rdquo;, and &ldquo;ghi&rdquo;.</p> <p>For the last group, we can only use the character &lsquo;j&rsquo; from the string.</p> <p>To complete this group, we add &lsquo;x&rsquo; twice. Thus, the 4 groups formed are &ldquo;abc&rdquo;, &ldquo;def&rdquo;, &ldquo;ghi&rdquo;, and &ldquo;jxx&rdquo;.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 100</code></li> <li><code>s</code> consists of lowercase English letters only.</li> <li><code>1 <= k <= 100</code></li> <li><code>fill</code> is a lowercase English letter.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • divideString

      public String[] divideString(String s, int k, char fill)