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

Source: leetcode | Difficulty: EASY
Tags: String algorithms

Starter code: class Solution:
    def lengthOfLastWord(self, s: str) -> int:
        

Solutions:
class Solution:

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


class Solution:

	def lengthOfLastWord(self, s):
		count = len(s.strip().split(' ')[-1])
		return count


class Solution:

	def lengthOfLastWord(self, s):
		(ans, tail) = (0, len(s) - 1)
		while tail >= 0 and s[tail] == ' ':
			tail -= 1
		while tail >= 0 and s[tail] != ' ':
			ans += 1
			tail -= 1
		return ans


class Solution:

	def lengthOfLastWord(self, s):
		if not s:
			return 0
		s = s.rstrip()
		s = s.lstrip()
		for i in range(0, len(s))[::-1]:
			if s[i] == ' ':
				return len(s) - 1 - i
		return len(s)


class Solution:

	def lengthOfLastWord(self, s):
		spaces = 0
		word_started = False
		for i in range(1, len(s) + 1):
			if s[-i] == ' ' and word_started is False:
				spaces += 1
			elif s[-i] == ' ' and word_started is True:
				return i - 1 - spaces
			else:
				word_started = True
		if word_started is True:
			return len(s) - spaces
		else:
			return 0


class Solution:

	def lengthOfLastWord(self, s):
		words = s.split()
		if not words:
			return 0
		return len(words[-1])


class Solution:

	def lengthOfLastWord(self, s):
		res = 0
		last = 0
		for i in s:
			if i == ' ':
				res = 0
			else:
				res += 1
				last = res
		return last


class Solution:

	def lengthOfLastWord(self, s):
		l = s.split(' ')
		while l:
			if l[-1]:
				return len(l[-1])
			else:
				l.pop(-1)
		return 0


class Solution:

	def lengthOfLastWord(self, s):
		v = s.split()
		if not v:
			return 0
		else:
			return len(v[-1])


class Solution:

	def lengthOfLastWord(self, s):
		l = len(s)
		n_c = 0
		for i in range(l - 1, -1, -1):
			if s[i] != ' ':
				n_c += 1
			elif s[i] == ' ' and n_c == 0:
				pass
			elif s[i] == ' ' and n_c != 0:
				return n_c
		return n_c


class Solution:

	def lengthOfLastWord(self, s):
		s = s.strip()
		if s == None:
			return 0
		count = 0
		for item in s[-1::-1]:
			if item == ' ':
				break
			count += 1
		return count