aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-06-21 23:23:54 -0600
committerJack Sangdahl <[email protected]>2025-06-21 23:23:54 -0600
commit4295d35ad283c079cd30f131bda21c3dd5d49896 (patch)
tree91e118faa4c9335dcd93cca94105002c5096702a
parentab3ffb04a9356621bf2ed0e535054095e27d312d (diff)
downloadleetcode-4295d35ad283c079cd30f131bda21c3dd5d49896.tar.gz
leetcode-4295d35ad283c079cd30f131bda21c3dd5d49896.zip
2138. divide a string into groups of size k
-rw-r--r--divide_string_groups_k.c47
-rw-r--r--divide_string_groups_k.py42
2 files changed, 89 insertions, 0 deletions
diff --git a/divide_string_groups_k.c b/divide_string_groups_k.c
new file mode 100644
index 0000000..1565b83
--- /dev/null
+++ b/divide_string_groups_k.c
@@ -0,0 +1,47 @@
+// 2138. Divide a String Into Groups of Size k
+#include "leetcode.h"
+
+/*
+ * 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,
+ * ans so on. each element 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, and concatenating
+ * all the groups in oder, the resultant string should be 's'. given a 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.
+ */
+
+char **divideString(char *s, int k, char fill, int *returnSize) {
+ int n = strlen(s), groups = (n + k - 1) / k;
+ char **ans = (char **)malloc(groups * sizeof(char *));
+ *returnSize = groups;
+ for (int i = 0; i < groups; i++) {
+ ans[i] = (char *)malloc((k + 1) * sizeof(char));
+ for (int j = 0; j < k; j++) {
+ int idx = i * k + j;
+ ans[i][j] = idx < n ? s[idx] : fill;
+ }
+ ans[i][k] = '\0';
+ }
+ return ans;
+}
+
+int main() {
+ char *s1 = "abcdefghi", *s2 = "abcdefghij";
+ int rs1, rs2;
+ char **ds1 = divideString(s1, 3, 'x', &rs1);
+ char **ds2 = divideString(s2, 3, 'x', &rs2);
+ for (int i = 0; i < rs1; i++)
+ printf("%s ", ds1[i]); // expect: ["abc","def","ghi"]
+ printf("\n");
+ for (int i = 0; i < rs2; i++)
+ printf("%s ", ds2[i]); // expect: ["abc","def","ghi","jxx"]
+ printf("\n");
+ for (int i = 0; i < rs1; i++)
+ free(ds1[i]);
+ for (int i = 0; i < rs2; i++)
+ free(ds2[i]);
+}
diff --git a/divide_string_groups_k.py b/divide_string_groups_k.py
new file mode 100644
index 0000000..2158483
--- /dev/null
+++ b/divide_string_groups_k.py
@@ -0,0 +1,42 @@
+# 2138. Divide a String Into Groups of Size k
+
+"""
+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,
+ans so on. each element 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, and concatenating
+all the groups in oder, the resultant string should be 's'. given a 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.
+"""
+
+
+class Solution(object):
+ def divideString(self, s, k, fill):
+ """
+ :type s: str
+ :type k: int
+ :type fill: str
+ :rtype: List[str]
+ """
+ n = len(s)
+ groups, ans = (n + k - 1) // k, []
+ for i in range(groups):
+ curr = ""
+ for j in range(k):
+ idx = i * k + j
+ if idx < n:
+ curr += s[idx]
+ else:
+ curr += fill
+ ans.append(curr)
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.divideString(s="abcdefghi", k=3, fill="x"))
+ print(obj.divideString(s="abcdefghij", k=3, fill="x"))