2138. Divide a String Into Groups of Size k Easy

@problem@discussion
#String#Simulation



1/**
2 * [2138] Divide a String Into Groups of Size k
3 *
4 * A string s can be partitioned into groups of size k using the following procedure:
5 * 
6 * 	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.
7 * 	For the last group, if the string does not have k characters remaining, a character fill is used to complete the group.
8 * 
9 * 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.
10 * 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.
11 *  
12 * Example 1:
13 * 
14 * Input: s = "abcdefghi", k = 3, fill = "x"
15 * Output: ["abc","def","ghi"]
16 * Explanation:
17 * The first 3 characters "abc" form the first group.
18 * The next 3 characters "def" form the second group.
19 * The last 3 characters "ghi" form the third group.
20 * Since all groups can be completely filled by characters from the string, we do not need to use fill.
21 * Thus, the groups formed are "abc", "def", and "ghi".
22 * 
23 * Example 2:
24 * 
25 * Input: s = "abcdefghij", k = 3, fill = "x"
26 * Output: ["abc","def","ghi","jxx"]
27 * Explanation:
28 * Similar to the previous example, we are forming the first three groups "abc", "def", and "ghi".
29 * For the last group, we can only use the character 'j' from the string. To complete this group, we add 'x' twice.
30 * Thus, the 4 groups formed are "abc", "def", "ghi", and "jxx".
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	1 <= s.length <= 100
36 * 	s consists of lowercase English letters only.
37 * 	1 <= k <= 100
38 * 	fill is a lowercase English letter.
39 * 
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/
44// discuss: https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49    pub fn divide_string(s: String, k: i32, fill: char) -> Vec<String> {
50        vec![]
51    }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_2138() {
62    }
63}
64


Back
© 2025 bowen.ge All Rights Reserved.