-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2138-DivideAStringIntoGroupsOfSizeK.go
More file actions
94 lines (82 loc) · 3.92 KB
/
2138-DivideAStringIntoGroupsOfSizeK.go
File metadata and controls
94 lines (82 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
// 2138. Divide a String Into Groups of Size k
// A string s can be partitioned into groups of size k using the following procedure:
// 1. 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.
// 2. 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.
import "fmt"
import "strings"
func divideString(s string, k int, fill byte) []string {
res, arr := []string{}, []byte(s)
for (len(arr) % k) != 0 {
arr = append(arr, fill)
}
for i := 0; i <= len(arr) - k; i += k {
res = append(res, string(arr[i:i + k]))
}
return res
}
func divideString1(s string, k int, fill byte) []string {
res, n := make([]string, 0), len(s)
for i := 0; i < n; i += k {
if i + k <= n {
res = append(res, s[i : i + k])
} else {
res = append(res, s[i : n] + strings.Repeat(string(fill), k - (n - i)))
}
}
return res
}
func main() {
// 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".
fmt.Println(divideString("abcdefghi", 3, 'x')) // ["abc","def","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".
fmt.Println(divideString("abcdefghij", 3, 'x')) // ["abc","def","ghi","jxx"]
fmt.Println(divideString("bluefrog", 3, 'x')) // [blu efr ogx]
fmt.Println(divideString("leetcode", 3, 'x')) // [lee tco dex]
fmt.Println(divideString1("abcdefghi", 3, 'x')) // ["abc","def","ghi"]
fmt.Println(divideString1("abcdefghij", 3, 'x')) // ["abc","def","ghi","jxx"]
fmt.Println(divideString1("bluefrog", 3, 'x')) // [blu efr ogx]
fmt.Println(divideString1("leetcode", 3, 'x')) // [lee tco dex]
}