Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Wednesday, July 12, 2017

Length of Last Word Interviewbit solution


Idea is to get last index of a whitespace and return length-lastindex-1

-1 is deducted because access starts from 0 index.

Code:

public class Solution {
public int lengthOfLastWord(final String a) {
   String temp=new String(a.trim());
   int len=temp.length();
   return(len-temp.lastIndexOf(" ")-1);
}
}

0 Comments:

Post a Comment

Stats