Chunks From the Left
Cut s into pieces of k characters each, starting from the left, and return the pieces as a list in order. The last piece is shorter than k when the length of s is not a multiple of k; it is never padded. An empty s gives an empty list.
Example 1
s "abcdefg"
k 3
returns ["abc","def","g"]
Seven characters cut into threes give abc, def and the leftover g.
Example 2
s "abcdef"
k 2
returns ["ab","cd","ef"]
Six characters cut into twos give exactly three full pieces.
Constraints
0 <= len(s) <= 601 <= k <= 10shas lowercase letters and digits only
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Strings problems
- Consecutive CharactersBeginner
- Count Capitalized WordsBeginner
- Count Differing PositionsBeginner
- Count Prefixes of a Given StringBeginner
- Defanging an IP AddressBeginner
- Determine if String Halves Are AlikeBeginner
- Find Words Containing CharacterBeginner
- Join the Non-Empty PartsBeginner