返回介绍

solution / 2100-2199 / 2138.Divide a String Into Groups of Size k / README_EN

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

2138. Divide a String Into Groups of Size k

中文文档

Description

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.

Solutions

Solution 1

class Solution:
  def divideString(self, s: str, k: int, fill: str) -> List[str]:
    return [s[i : i + k].ljust(k, fill) for i in range(0, len(s), k)]
class Solution {
  public String[] divideString(String s, int k, char fill) {
    int n = s.length();
    String[] ans = new String[(n + k - 1) / k];
    if (n % k != 0) {
      s += String.valueOf(fill).repeat(k - n % k);
    }
    for (int i = 0; i < ans.length; ++i) {
      ans[i] = s.substring(i * k, (i + 1) * k);
    }
    return ans;
  }
}
class Solution {
public:
  vector<string> divideString(string s, int k, char fill) {
    int n = s.size();
    if (n % k)
      for (int i = 0; i < k - n % k; ++i) s.push_back(fill);
    vector<string> ans;
    for (int i = 0; i < s.size() / k; ++i) ans.push_back(s.substr(i * k, k));
    return ans;
  }
};
func divideString(s string, k int, fill byte) []string {
  n := len(s)
  if n%k != 0 {
    s += strings.Repeat(string(fill), k-n%k)
  }
  var ans []string
  for i := 0; i < len(s)/k; i++ {
    ans = append(ans, s[i*k:(i+1)*k])
  }
  return ans
}

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

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

发布评论

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