diff --git a/leetcode/2101-2200/2138.Divide-a-String-Into-Groups-of-Size-k/README.md b/leetcode/2101-2200/2138.Divide-a-String-Into-Groups-of-Size-k/README.md index 0a96d5acb..e584b2a5c 100755 --- a/leetcode/2101-2200/2138.Divide-a-String-Into-Groups-of-Size-k/README.md +++ b/leetcode/2101-2200/2138.Divide-a-String-Into-Groups-of-Size-k/README.md @@ -1,28 +1,39 @@ # [2138.Divide a String Into Groups of Size k][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## 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 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 (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: a = "11", b = "1" -Output: "100" +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:** -### 思路1 -> ... -Divide a String Into Groups of Size k -```go ``` - +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". +``` ## 结语 diff --git a/leetcode/2101-2200/2138.Divide-a-String-Into-Groups-of-Size-k/Solution.go b/leetcode/2101-2200/2138.Divide-a-String-Into-Groups-of-Size-k/Solution.go index d115ccf5e..59315e771 100644 --- a/leetcode/2101-2200/2138.Divide-a-String-Into-Groups-of-Size-k/Solution.go +++ b/leetcode/2101-2200/2138.Divide-a-String-Into-Groups-of-Size-k/Solution.go @@ -1,5 +1,17 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string, k int, fill byte) []string { + l := len(s) + bs := []byte(s) + mod := l % k + if mod != 0 { + for i := 0; i < k-mod; i++ { + bs = append(bs, fill) + } + } + var ans []string + for i := 0; i < len(bs); i += k { + ans = append(ans, string(bs[i:i+k])) + } + return ans } diff --git a/leetcode/2101-2200/2138.Divide-a-String-Into-Groups-of-Size-k/Solution_test.go b/leetcode/2101-2200/2138.Divide-a-String-Into-Groups-of-Size-k/Solution_test.go index 14ff50eb4..3a7337193 100644 --- a/leetcode/2101-2200/2138.Divide-a-String-Into-Groups-of-Size-k/Solution_test.go +++ b/leetcode/2101-2200/2138.Divide-a-String-Into-Groups-of-Size-k/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + k int + fill byte + expect []string }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "abcdefghi", 3, 'x', []string{"abc", "def", "ghi"}}, + {"TestCase2", "abcdefghij", 3, 'x', []string{"abc", "def", "ghi", "jxx"}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.k, c.fill) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v", + c.expect, got, c.inputs, c.k, c.fill) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }