[
string
]
Leetcode 0806 Number of Lines To Write String
Problem statement
https://leetcode.com/problems/number-of-lines-to-write-string/
Solution
Just iterate symbol by symbol and if we do not have place at some string, go to the next one.
Complexity
Time complexity is O(n)
, where n
is length of S
.
Code
class Solution:
def numberOfLines(self, widths, S):
x, y = 0, 1
for symb in S:
w = widths[ord(symb) - ord("a")]
if x + w <= 100:
x += w
else:
x, y = w, y + 1
return [y, x]