Length of Last Word

const lengthOfLastWord = function(s) { let length = 0; // Iterate backward through the string for (let i = s.length – 1; i >= 0; i–) { // Count characters until a space is encountered if (s[i] !== ” “) { length++; } else if (length > 0) { // Break loop when last word…

By.

min read

Amazon
const lengthOfLastWord = function(s) {
    let length = 0;
    // Iterate backward through the string
    for (let i = s.length - 1; i >= 0; i--) {
        // Count characters until a space is encountered
        if (s[i] !== " ") {
            length++;
        } else if (length > 0) {
            // Break loop when last word is counted
            return length;
        }
    }
    // Return length of the last word
    return length;
};
// Example usage
console.log(lengthOfLastWord("Length of last word"));

Explanation:

  • The function iterates backward through the input string.
  • It counts characters until a space is encountered.
  • When a space is found after counting characters, it returns the length of the last word.
  • The loop continues until the entire string is traversed, providing the length of the last word.

Approach 1: String Index Manipulation

Overview: Locate the last word in a string, then count its length.

Algorithm:

  1. Iterate backward to find the last non-space character, marking the end of the last word.
  2. Count the length of the last word.
const lengthOfLastWord = function(s) {
    // Trim the trailing spaces
    let p = s.length - 1;
    while (p >= 0 && s[p] === ' ') {
        p--;
    }
    // Compute the length of the last word
    let length = 0;
    while (p >= 0 && s[p] !== ' ') {
        p--;
        length++;
    }
    return length;
};
console.log(lengthOfLastWord("Length of last word"));

Complexity:

  • Time: O(N), where N is the string length.
  • Space: O(1), constant memory.

Approach 2: One-loop Iteration

Overview: Combine locating the last word and counting its length in a single loop.

Algorithm: Use a loop to iterate, defining the moment to start counting the word’s length.

const lengthOfLastWord = function(s) {
    let totalLength = s.length;
    let length =0
    while(totalLength >0){
        totalLength--;
        if(s[totalLength]!==" "){
            length++;
        }else if(length > 0){
            return length;
        }
    }
    return length;
};
console.log(lengthOfLastWord("Length of last word"));

Complexity:

  • Time: O(N), where N is the string length.
  • Space: O(1), constant memory.

Approach 3: Built-in String Functions

Overview: Utilize built-in functions for string manipulation.

Algorithm:

  • In Javascript: Use String.trim(), String.length(), and String.lastIndexOf(char).
const lengthOfLastWord = function(s) {
    s = s.trim(); // Trim the trailing spaces in the string
    return s.length - s.lastIndexOf(" ") - 1;
};
console.log(lengthOfLastWord("Length of last word"));

Complexity:

  • Time: O(N), where N is the string length.
  • Space: O(N), as some functions may require additional memory.

Conclusion: These approaches offer efficient solutions, with the choice depending on language capabilities and preference.

Leave a Reply

Your email address will not be published. Required fields are marked *