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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s