LeetCode Js-58. Length of Last Word

    LeetCode Js-String

    Given a string s consisting of words and spaces, return the length of the last word in the string.
    A word is a maximal substring consisting of non-space characters only.

    給予一個字串 s 是由字母與空白所組成,回傳最後一個字母的字串長度。
    單一個字母是僅由非空格字符組成的最大字串。
    

    Example 1:

    Input: s = "Hello World"
    Output: 5
    Explanation: The last word is "World" with length 5.
    

    Example 2:

    Input: s = "   fly me   to   the moon  "
    Output: 4
    Explanation: The last word is "moon" with length 4.
    

    Example 3:

    Input: s = "luffy is still joyboy"
    Output: 6
    Explanation: The last word is "joyboy" with length 6.
    

    solution:
    1. 宣告一個 array 變數,運用 regex 來處理單一空白與多個空白,並透過 split 來進行切割。
    2. 如果 s 的長度為 0,代表不存在文字,故回傳 0。
    3. 如果 array 的長度為 0,代表不存在文字,故回傳 0。
    4. 如果 array 存在文字則判斷,
    4-1 宣告 lastWord 變數為 array.pop(),該陣列中最後一個元素。
    4-2 如果這個 lastWord 存在長度,則回傳 lastWord.length。
    5. 回傳 0。

    Code 1: BigO(n)

    var lengthOfLastWord = function(s) {
        let array = s.split(/\s/); 
    
        if(s.length === 0) return 0;
        if(array.length === 0) return 0;
    
        while(array.length > 0){
            let lastWord = array.pop();
            if(lastWord.length > 0) return lastWord.length;
        }
        return 0;
    };
    

    FlowChart:
    Example 1

    Input: s = "Hello World"
    
    array = s.split(/\s/) //["Hello", "World"]
    lastWord = World
    lastWord.length = 5
    return 5;
    

    Example 2

    Input: s = "   fly me   to   the moon  "
    
    array = s.split(/\s/) //["fly", "me", "to", "the", "moon"]
    lastWord = moon
    lastWord.length = 4
    return 4;
    

    Example 3

    Input: s = "luffy is still joyboy"
    
    array = s.split(/\s/) //["luffy", "is", "still", "joyboy"]
    lastWord = joyboy
    lastWord.length = 6
    return 6;
    

    Code 2: BigO(n)

    var lengthOfLastWord = function(s) {
        let array = s.trim().split(" ")
        return array[array.length - 1].length;
    };
    

    Code 3: BigO(n)

    var lengthOfLastWord = function(s) {
        let array = s.trim().split(/\s/)
        return array[array.length - 1].length;
    };
    

    String.prototype.trim(): 移除字首和字尾的空白。

    const greeting = '   Hello world!   ';
    
    console.log(greeting);
    // Expected output: "   Hello world!   ";
    
    console.log(greeting.trim());
    // Expected output: "Hello world!";
    

    Array.prototype.pop()

    const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
                                                                      ^
    console.log(plants.pop());
    // Expected output: "tomato"
    
    console.log(plants);
    // Expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
                                                                        ^
    plants.pop();
    
    console.log(plants);
    // Expected output: Array ["broccoli", "cauliflower", "cabbage"]
    

    String.split(/\s+/)

    regex => /rules/
    regex => \s+
    ex. 至少一個空白或多個空白。
    
    regex => /\s+/
    ex.呼叫正則函數來判斷至少一個空白或同時多個空白。
    

    正則表達式(Regular Expression,簡稱 RegExp)是用來描述字串模式的工具,可以用來搜尋、匹配、替換等操作。以下是一些常見的正則表達式符號與用法:

    字符符號:
    .:匹配任何單個字符,但不包括換行符。
    \w:匹配任何字母、數字、底線字符。相當於 [a-zA-Z0-9_]。
    \W:匹配任何非字母、非數字、非底線字符。
    \d:匹配任何數字字符。相當於 [0-9]。
    \D:匹配任何非數字字符。
    \s:匹配任何空白字符,包括空格、制表符、換行符等。
    \S:匹配任何非空白字符。
    重複符號:
    *:匹配前面的元素零次或多次。
    +:匹配前面的元素一次或多次。
    ?:匹配前面的元素零次或一次。
    {n}:匹配前面的元素恰好 n 次。
    {n,}:匹配前面的元素至少 n 次。
    {n,m}:匹配前面的元素至少 n 次,但不超過 m 次。
    範圍符號:
    [abc]:匹配字符 a、b 或 c 中的任意一個。
    [a-z]:匹配小寫字母 a 到 z 中的任意一個。
    [A-Z]:匹配大寫字母 A 到 Z 中的任意一個。
    [0-9]:匹配數字 0 到 9 中的任意一個。
    邏輯運算符:
    |:表示 OR 運算,匹配左右兩邊其中之一。
    ():分組,用來限定運算符的作用範圍。
    ^:匹配字串開頭。
    $:匹配字串結尾。
    轉義符號:
    \:用來轉義特殊字符,使其失去其特殊含義。