leetcodedaybyday - Beats 100% with C++ and Python3
IntuitionThe task is to divide a string s into chunks of size k. If the length of s is not a multiple of k, the final chunk must be filled with a specified char
# **Intuition**
The task is to divide a string `s` into chunks of size `k`. If the length of `s` is not a multiple of `k`, the final chunk must be filled with a specified character (`fill`) to make it exactly of length `k`. This problem can be solved by iterating through the string and forming chunks sequentially.

---

# **Approach**

1. **Iterate Through the String**:
   - Use a loop to iterate through each character in the string `s`.
   - Append characters to a temporary string (`temp`) until its size reaches `k`.

2. **Store Complete Chunks**:
   - Once `temp` reaches the size `k`, add it to the result list (`ans`), and reset `temp` for the next chunk.

3. **Handle Remaining Characters**:
   - If there are leftover characters in `temp` after the loop, pad the string with the `fill` character until its size is `k`.
   - Add the padded string to `ans`.

4. **Return Result**:
   - Return the list of chunks as the final result.

---

# **Complexity**

- **Time Complexity**:  
   \( O(n) \), where \( n \) is the length of the string `s`. Each character is processed once.

- **Space Complexity**:  
   \( O(n) \), for the storage of the result list, which contains all chunks of the string.

---

# Code
```cpp []
class Solution {
public:
    vector<string> divideString(string s, int k, char fill) {
        int n = s.length();
        vector<string> ans;
        string temp = "";
        int j = 0;
        for (int i = 0; i < n; i++){
            temp += s[i];
            j += 1;
            if (j == k){
                ans.push_back(temp);
                temp.clear();
                j = 0;
            }
        }
        if (temp.length() != 0){
            for (int i = 0; i < k - j; i++){
                temp += fill;
            }
            ans.push_back(temp);
            temp.clear();
        }
        return ans;
    }
};
```
```python3 []
class Solution:
    def divideString(self, s: str, k: int, fill: str) -> List[str]:
        n = len(s)
        ans = []
        temp = ""
        j = 0
        for i in range(n):
            temp += s[i]
            j += 1
            if j == k:
                ans.append(temp)
                temp = ""
                j = 0
        if len(temp) != 0:
            temp += fill * (k - j)
            ans.append(temp)
        return ans
```

['C++', 'Python3']