LeetCode September Challenge(Day-15)

Asish Raz
Oct 16, 2020
monthly challenge

Topic — Length of Last Word

Q. Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.

If the last word does not exist, return 0.

Note: A word is defined as a maximal substring consisting of non-space characters only.

Example:

Input: "Hello World"
Output: 5

Logic:

  • First split the string and check whether the length of the string after splitting is zero or not.
  • If not zero,then directly return the length of the last word.

Code:

code

Happy Coding!!!

Here is the leetcode link:
https://leetcode.com/explore/challenge/card/september-leetcoding-challenge/556/week-3-september-15th-september-21st/3461/

Please feel free to comment, if you have any problem or doubt.

Thank You!!!
Never settle and always Hustle!!!

-Gareeb CODER

(Thanks for your time and do encourage me to write more by clapping.)

--

--

Asish Raz

entrepreneur, moody, moody writer, moody singer, traveller, hangout lover