Solution 1 - Optimized
Scan from the end, skipping trailing spaces, then count the final word.
Scan from the end, skipping trailing spaces, then count the final word.
""" 0058.1 - Length of Last Word - Solution 1 - Optimized """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def lengthOfLastWord(self, s: str) -> int:
"""Length of Last Word Function"""
return len(s.rstrip().split()[-1])
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
assert Solution().lengthOfLastWord("Hello World") == 5
assert Solution().lengthOfLastWord(" fly me to the moon ") == 4
assert Solution().lengthOfLastWord("luffy is still joyboy") == 6
print("tests passed")
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()/** 0058.1 - Length of Last Word - Solution 1 - Optimized */
/////////////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////////////
class Solution {
/** Length of Last Word Function */
lengthOfLastWord(s) {
return s.trim().split(/\s+/).pop().length;
}
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
function testcase() {
/** Test Function */
const solution = new Solution();
console.assert(solution.lengthOfLastWord("Hello World") === 5);
console.assert(solution.lengthOfLastWord(" fly me to the moon ") === 4);
console.assert(solution.lengthOfLastWord("luffy is still joyboy") === 6);
console.log("tests passed");
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
testcase();/** 0058.1 - Length of Last Word - Solution 1 - Optimized */
/////////////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////////////
class Solution {
/** Length of Last Word Function */
lengthOfLastWord(s: string): number {
return s.trim().split(/\s+/).pop()!.length;
}
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
function testcase(): void {
/** Test Function */
const solution = new Solution();
console.assert(solution.lengthOfLastWord("Hello World") === 5);
console.assert(solution.lengthOfLastWord(" fly me to the moon ") === 4);
console.assert(solution.lengthOfLastWord("luffy is still joyboy") === 6);
console.log("tests passed");
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
testcase();// 0058.1 - Length of Last Word - Solution 1 - Optimized
package main
/////////////////////////////////////////////////////////////////////////////////////
// Imports
/////////////////////////////////////////////////////////////////////////////////////
import (
"fmt"
"strings"
)
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
func lengthOfLastWord(s string) int {
parts := strings.Fields(s)
return len(parts[len(parts)-1])
}
func testcase() {
// Test Function
fmt.Println(lengthOfLastWord("Hello World") == 5)
fmt.Println(lengthOfLastWord(" fly me to the moon ") == 4)
fmt.Println(lengthOfLastWord("luffy is still joyboy") == 6)
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
func main() {
testcase()
}// 0058.1 - Length of Last Word - Solution 1 - Optimized
/////////////////////////////////////////////////////////////////////////////////////
// Structs
/////////////////////////////////////////////////////////////////////////////////////
struct Solution;
/////////////////////////////////////////////////////////////////////////////////////
// Implementations
/////////////////////////////////////////////////////////////////////////////////////
impl Solution {
/// Length of Last Word Function
pub fn length_of_last_word(s: String) -> i32 {
s.split_whitespace().last().unwrap().len() as i32
}
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
fn testcase() {
// Test Function
assert_eq!(Solution::length_of_last_word("Hello World".into()), 5);
assert_eq!(Solution::length_of_last_word(" fly me to the moon ".into()), 4);
assert_eq!(Solution::length_of_last_word("luffy is still joyboy".into()), 6);
println!("tests passed");
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
fn main() {
testcase();
}