FAILED Scenario for "a " Expected : 1 , Output 0; HOW?? a is not the last word..


  • -1
    W

    public class Solution {
    public int lengthOfLastWord(String s) {
    int count=0;
    int len = s.length();
    char p[] =s.toCharArray();
    if(len>0)
    {
    for(int i = len-1; i>=0 ;i-- )
    {
    if(i==0)
    { if(p[i] == ' ')
    return count;
    else
    return len;
    }
    else if (p[i] != ' ')
    {
    count = count+1;
    }
    else if (p[i] == ' ')
    {
    return count;
    }
    }
    }
    else
    {
    return 0;
    }
    return 0;}
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.