#58 Length of Last Word

﹥>﹥吖頭↗ 提交于 2019-12-04 14:15:15

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

Example:

Input: "Hello World"
Output: 5

Solution1;

class Solution:
    def lengthOfLastWord(self, s: str) -> int:
        if s:
            return len(s.strip().split(" ")[-1])
        else:
            return 0
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!