본문 바로가기
Beakjoon&프로그래머스/Java

[LeetCode/Java] 58. Length of Last Word

by 현장 2025. 11. 20.

-Code

class Solution {
    public int lengthOfLastWord(String s) {
        // 스페이스를 기준으로 나눠서 배열 생성
        String[] strList = s.split(" ");
        // 리스트의 마지막 문자열 가져옴
        String lastStr = strList[strList.length - 1];
        // 마지막 문자열의 길이 반환
        return lastStr.length();
    }
}