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);
}
}
-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