跳至主要內容

2138. 将字符串拆分为若干长度为 k 的组


2138. 将字符串拆分为若干长度为 k 的组

🟢   🔖  字符串 模拟  🔗 力扣open in new window LeetCodeopen in new window

题目

A string s can be partitioned into groups of size k using the following procedure:

  • The first group consists of the first k characters of the string, the second group consists of the next k characters of the string, and so on. Each character can be a part of exactly one group.
  • For the last group, if the string does not have k characters remaining, a character fill is used to complete the group.

Note that the partition is done so that after removing the fill character from the last group (if it exists) and concatenating all the groups in order, the resultant string should be s.

Given the string s, the size of each group k and the character fill, return _a string array denoting the composition of every group _s has been divided into, using the above procedure.

Example 1:

Input: s = "abcdefghi", k = 3, fill = "x"

Output: ["abc","def","ghi"]

Explanation:

The first 3 characters "abc" form the first group.

The next 3 characters "def" form the second group.

The last 3 characters "ghi" form the third group.

Since all groups can be completely filled by characters from the string, we do not need to use fill.

Thus, the groups formed are "abc", "def", and "ghi".

Example 2:

Input: s = "abcdefghij", k = 3, fill = "x"

Output: ["abc","def","ghi","jxx"]

Explanation:

Similar to the previous example, we are forming the first three groups "abc", "def", and "ghi".

For the last group, we can only use the character 'j' from the string. To complete this group, we add 'x' twice.

Thus, the 4 groups formed are "abc", "def", "ghi", and "jxx".

Constraints:

  • 1 <= s.length <= 100
  • s consists of lowercase English letters only.
  • 1 <= k <= 100
  • fill is a lowercase English letter.

题目大意

字符串 s 可以按下述步骤划分为若干长度为 k 的组:

  • 第一组由字符串中的前 k 个字符组成,第二组由接下来的 k 个字符串组成,依此类推。每个字符都能够成为 某一个 组的一部分。
  • 对于最后一组,如果字符串剩下的字符 不足 k 个,需使用字符 fill 来补全这一组字符。

注意,在去除最后一个组的填充字符 fill(如果存在的话)并按顺序连接所有的组后,所得到的字符串应该是 s

给你一个字符串 s ,以及每组的长度 k 和一个用于填充的字符 fill ,按上述步骤处理之后,返回一个字符串数组,该数组表示 s 分组后 每个组的组成情况

示例 1:

输入: s = "abcdefghi", k = 3, fill = "x"

输出:["abc","def","ghi"]

解释:

前 3 个字符是 "abc" ,形成第一组。

接下来 3 个字符是 "def" ,形成第二组。

最后 3 个字符是 "ghi" ,形成第三组。

由于所有组都可以由字符串中的字符完全填充,所以不需要使用填充字符。

因此,形成 3 组,分别是 "abc"、"def" 和 "ghi" 。

示例 2:

输入: s = "abcdefghij", k = 3, fill = "x"

输出:["abc","def","ghi","jxx"]

解释:

与前一个例子类似,形成前三组 "abc"、"def" 和 "ghi" 。

对于最后一组,字符串中仅剩下字符 'j' 可以用。为了补全这一组,使用填充字符 'x' 两次。

因此,形成 4 组,分别是 "abc"、"def"、"ghi" 和 "jxx" 。

提示:

  • 1 <= s.length <= 100
  • s 仅由小写英文字母组成
  • 1 <= k <= 100
  • fill 是一个小写英文字母

解题思路

  1. 初始化结果数组

    • 使用 res 数组来存储每一个分割的子字符串。
  2. 按块分割字符串

    • 遍历字符串 s,按照每块长度为 k 的方式分割,使用 slice 截取子字符串,依次加入结果数组。
  3. 处理不足块

    • 如果字符串的长度不是 k 的倍数,需要填充字符。
    • slice 获取最后一部分字符串,加上 fill 重复至所需长度。
  4. 返回结果数组

    • 最后返回包含所有子字符串的数组 res

复杂度分析

  • 时间复杂度O(n),其中 n 是字符串的长度,分割字符串耗时。
  • 空间复杂度O(n / k),结果数组 res 占用的空间。

代码

/**
 * @param {string} s
 * @param {number} k
 * @param {character} fill
 * @return {string[]}
 */
var divideString = function (s, k, fill) {
	let res = [];
	let i = 0;

	// 按 k 长度分割字符串
	while (i < s.length) {
		res.push(s.slice(i, i + k));
		i += k;
	}

	// 补齐最后一部分字符串
	const last = res[res.length - 1];
	if (last.length < k) {
		res[res.length - 1] = last + fill.repeat(k - last.length);
	}

	return res;
};

相关题目

题号标题题解标签难度力扣
68文本左右对齐[✓]数组 字符串 模拟🔴🀄️open in new window 🔗open in new window
830较大分组的位置[✓]字符串🟢🀄️open in new window 🔗open in new window