Find Length of the Last Word

medium

By - Aman Pareek

Last Updated - 31/08/2024

Problem Statement

Given a string s consisting of words separated by spaces, you need to determine the length of the last word in the string. The string may contain leading and trailing spaces, and words are separated by one or more spaces.

Example 1

Input: str = "hello aman"

Output: 4

Solution 1: Using String Methods

function getLengthOfLastWord(s) {
    if (typeof s !== 'string') throw new TypeError('Input must be a string');
    
    s = s.trim();
    const words = s.split(' ');
    return words[words.length - 1].length;
} 

const str1 = "hello aman";
getLengthOfLastWord(str1);  //output: 4 

Solution 2: Using Regular Expressions

function findLengthOfLastWordUsingRegex(s) {
    if (typeof s !== 'string') throw new TypeError('Input must be a string');
    
    const match = s.trim().match(/\S+$/);
    return match ? match[0].length : 0;
} 

const str1 = "hello aman";
findLengthOfLastWordUsingRegex(str1);  //output: 4 

Solution 3: Manual Traversal

function calculateLengthOfLastWord(s) {
    if (typeof s !== 'string') throw new TypeError('Input must be a string');
    
    let length = 0;
    let i = s.length - 1;

    while (i >= 0 && s[i] === ' ') {
        i--;
    }

    while (i >= 0 && s[i] !== ' ') {
        length++;
        i--;
    }

    return length;
} 

const str1 = "hello aman";
calculateLengthOfLastWord(str1);  //output: 4 

Solution 4: Using Array.prototype.reduce

function lengthOfLastWordWithReduce(s) {
    if (typeof s !== 'string') throw new TypeError('Input must be a string');
    
    return s.trim().split(/\s+/).reduce((_, word) => word.length, 0);
} 

const str1 = "hello aman";
lengthOfLastWordWithReduce(str1);  //output: 4 

Solution 5: Using Array.prototype.forEach

function lengthOfLastWordUsingForEach(s) {
    if (typeof s !== 'string') throw new TypeError('Input must be a string');
    
    let lastWordLength = 0;
    s.trim().split(/\s+/).forEach(word => lastWordLength = word.length);
    return lastWordLength;
} 

const str1 = "hello aman";
lengthOfLastWordUsingForEach(str1);  //output: 4 

Solution 6: Using Array.prototype.pop

function getLastWordLength(s) {
    if (typeof s !== 'string') throw new TypeError('Input must be a string');
    
    const words = s.trim().split(/\s+/);
    return words.pop().length;
} 

const str1 = "hello aman";
getLastWordLength(str1);  //output: 4 

Solution 7: Using String.prototype.lastIndexOf

function lengthOfLastWordWithLastIndexOf(s) {
    if (typeof s !== 'string') throw new TypeError('Input must be a string');
    
    s = s.trim();
    const lastSpace = s.lastIndexOf(' ');
    return s.length - lastSpace - 1;
} 

const str1 = "hello aman";
lengthOfLastWordWithLastIndexOf(str1);  //output: 4 

Solution 8: Using String.prototype.split with Map

function lengthOfLastWordUsingMap(s) {
    if (typeof s !== 'string') throw new TypeError('Input must be a string');
    
    const words = s.trim().split(/\s+/);
    return words.map(word => word.length).pop();
} 

const str1 = "hello aman";
lengthOfLastWordUsingMap(str1);  //output: 4 

Solution 9: Using Stack Approach

function lengthOfLastWordUsingStack(s) {
    if (typeof s !== 'string') throw new TypeError('Input must be a string');
    
    const stack = [];
    for (const char of s.trim()) {
        if (char === ' ') {
            stack.length = 0;
        } else {
            stack.push(char);
        }
    }
    return stack.length;
} 

const str1 = "hello aman";
lengthOfLastWordUsingStack(str1);  //output: 4 

Solution 10: Using String.prototype.substring

function getLengthOfLastWordUsingSubstring(s) {
    if (typeof s !== 'string') throw new TypeError('Input must be a string');
    
    s = s.trim();
    const lastSpaceIndex = s.lastIndexOf(' ');
    return s.substring(lastSpaceIndex + 1).length;
} 

const str1 = "hello aman";
getLengthOfLastWordUsingSubstring(str1);  //output: 4 

Popular Solutions