Cheakout This Most Easy Solution In JAVA ---->


#1

A = A.trim() ;

    int count = 0;
    int n = A.length() ;
    
    if( !A.contains(" ") )
    return A.length() ;
    
    for( int i = n-1 ; i >= 0 ; i-- )
    {
        int ch = A.charAt(i) ;
        if( ch == ' ' )
        {
          return count ;
         
        }
        
        count++ ;
        
    }
    
    return 0 ;

#2

Actually you need 1 loop for space character in string


#3

public class Solution {
// DO NOT MODIFY THE ARGUMENTS WITH “final” PREFIX. IT IS READ ONLY
public int lengthOfLastWord(final String A) {
try {
String str[]=A.split(" ");
return str[str.length-1].length();
}
catch (Exception e)
{
return 0;
}
}
}[quote=“yuvrajbhati2507_192d, post:1, topic:47067, full:true”]
A = A.trim() ;

    int count = 0;
    int n = A.length() ;
    
    if( !A.contains(" ") )
    return A.length() ;
    
    for( int i = n-1 ; i >= 0 ; i-- )
    {
        int ch = A.charAt(i) ;
        if( ch == ' ' )
        {
          return count ;
         
        }
        
        count++ ;
        
    }
    
    return 0 ;

[/quote]

updated…


#4

But, why are we using the String Library Functions like trim(), split(), contains() . The problem statement clearly states that they should be avoided ??:thinking::thinking::thinking: