코린이의 소소한 공부노트

[LeetCode/Easy] 58. Length of Last Word 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 58. Length of Last Word

무지맘 2022. 8. 23. 23:06

1. Input

1) String 변수 s

2) s는 word와 space로 구성

3) word는 space로 구분됨

 

2. Output

1) s의 마지막 word의 길이

 

3. Constraint

1) 1 <= s.length <= 104

2) s는 영문자와 빈 공간으로만 구성되어 있음

3) s에는 최소 1개의 word가 있음

 

4. Example

Input: s = "   fly me   to   the moon  "

Output: 4

설명:

  - 빈 공간을 제외하면 words = "fly", "me", "to", "the", "moon"

  - 마지막 단어인 "moon"의 길이는 4

 

5. Code

1) 첫 코드(2022/05/31)

String[] str = s.split(" ");
return str[str.length-1].length();

  - s를 " "로 나눔

  - 배열의 가장 마지막 단어(str[str.length-1])의 길이를 반환해줌