A garment workshop embroiders a customer's short message onto a banner with a single automated needle, one letter at a time, left to right. Each of the 26 lowercase letters requires a fixed number of stitch-units of horizontal thread to embroider (letter 'a' has its own stitch-unit cost, letter 'b' has its own, and so on through 'z'). The embroidery hoop can hold at most 100 stitch-units of thread on any single row. Before embroidering the next letter, the machine checks whether adding that letter would push the current row's total strictly above 100 stitch-units; if so, it advances to a brand-new row and embroiders that letter there instead (starting the new row's total at that letter's own cost). Otherwise the letter is added to the current row.
Given the 26 stitch-unit costs and the message, determine how many rows the machine ends up using in total, and how many stitch-units are used on the final row.
The input consists of 27 whitespace-separated tokens. The first 26 are integers widths[0..25], where widths[i] is the stitch-unit cost of the letter at position i in the alphabet (widths[0] is 'a', widths[25] is 'z'). The 27th token is the message string s.
Print two space-separated integers: the total number of rows used, followed by the number of stitch-units used on the final row.
widths.length == 26, and 1 <= widths[i] <= 10 for every i.1 <= s.length <= 100, and s consists only of lowercase English letters.Example 1
Input
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 abcdefghijklmnopqrstuvwxyz
Expected
3 60
Explanation
Every letter costs 10 stitch-units, so a row holds at most 10 letters (10*10=100). The 26-letter message fills two full rows of 10 letters (100 stitch-units each) and leaves 6 letters for a third row, which uses 6*10=60 stitch-units. Total rows = 3, final-row stitch-units = 60.
Example 2
Input
4 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 bbbcccdddaaaa
Expected
2 8
Explanation
Here 'a' costs 4 stitch-units and every other letter costs 10. Embroidering b,b,b,c,c,c,d,d,d,a,a reaches a running total of 98 (nine 10-cost letters plus two 4-cost letters). The next 'a' would make 98+4=102>100, so a new row starts with that 'a' (cost 4), followed by the final 'a' (cost 4 more), giving a last-row total of 8. Total rows = 2, final-row stitch-units = 8.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →