Skip to content

Latest commit

 

History

History
35 lines (23 loc) · 915 Bytes

풀이_LC58.md

File metadata and controls

35 lines (23 loc) · 915 Bytes

🐁 LeetCode 58. Length of Last Word

  • Date : 2021.07.04(일)
  • Time : 20분

Problem

  • Given a string s consists of some words separated by spaces, return the length of the last word in the string. If the last word does not exist, return 0. A word is a maximal substring consisting of non-space characters only.

Constraints

  • 1 <= s.length <= 10^4
  • s consists of only English letters and spaces ' '.

Example

  • Input: s = "Hello World"
  • Output: 5

풀이

    def lengthOfLastWord(self, s: str) -> int:
        new_string = s.split()
        if new_string == [] :
            return 0
        else: return len(new_string[-1])

: 먼저 문자열에서 공백을 기준으로 문자열을 배열로 변경해준다. s = "Hello World" 였다면 new_string = ["Hello", "World"]로 생성된다. 이 때 마지막 배열의 길이를 return 해주면된다.