Unknown's avatar

806. Number of Lines To Write String

806. Number of Lines To Write String

class Solution {	public int[] numberOfLines(int[] widths, String S) {

		int currentChar = 0;
		if (S.length() == 0) return new int[]{0,0};
		int lines = 1;

		for (int i = 0; i < S.length(); i++) {
			currentChar += widths[S.charAt(i) - 97];
			if( currentChar > 100){
				currentChar -= widths[S.charAt(i) - 97];
				currentChar = widths[S.charAt(i) - 97];
				lines++;
			}else if( currentChar == 99 && i < S.length() - 1 /*at least there is one following char*/){
				currentChar = 0;
				lines++;
			}
		}

		return new int[] { lines, currentChar};

	}}

Leave a comment