본문 바로가기
Problem Solving/LeetCode

[LeetCode] 806. Number of Lines To Write String - Java

by graycode 2026. 10. 17.

• 문제 링크

 

Number of Lines To Write String - LeetCode

Can you solve this real interview question? Number of Lines To Write String - You are given a string s of lowercase English letters and an array widths denoting how many pixels wide each lowercase English letter is. Specifically, widths[0] is the width of

leetcode.com

 

• 풀이 코드

public class Solution {

    public int[] numberOfLines(int[] widths, String s) {
        int l = 1, w = 0;
        for (char c : s.toCharArray()) {
            int n = widths[c - 'a'];
            if (w + n > 100) {
                l++;
                w = n;
            } else w += n;
        }

        return new int[]{l, w};
    }

}

댓글