Wednesday, May 17, 2017

Length of last Word


Problem Statement

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.
Link to GitHub : Code

Given s = "Prathap Kudupu",
return 6.

Solution

  • The result  is the difference between the length of the string and and index of  empty space
     public static int optimum(String s)
 {
  return s.trim().length()-s.trim().indexOf(' ')-1;
 }

No comments:

Post a Comment